How to safely convert from a double to a decimal in c# -
we storing financial data in sql server database using decimal data type , need 6-8 digits of precision in decimal. when value through our data access layer our c# server, coming decimal data type.
due design constraints beyond control, needs converted. converting string isn't problem. converting double ms documentation says "[converting decimal double] can produce round-off errors because double-precision floating-point number has fewer significant digits decimal."
as double (or string) can round 2 decimal places after calculations done, "right" way decimal conversion ensure don't lose precision before rounding?
the conversion won't produce errors within first 8 digits. double
has 15-16 digits of precision - less 28-29 of decimal
, enough purposes sounds of it.
you should put in place sort of plan avoid using double
in future, - it's unsuitable datatype financial calculations.
Comments
Post a Comment