I am trying to convert a double to a decimal using Convert.ToDecimal function.
The problem is that somehow .net chooses to omit decimal digits depending on the size of original double variable.
Take the following example:
double d = 2999247013.972682;
decimal convDecimal = Convert.ToDecimal(d);
decimal realDecimal = 2999247013.972682M;
Console.WriteLine(d);
Console.WriteLine(convDecimal);
Console.WriteLine(realDecimal);
Which produces the following output:
2999247013,97268 // comments: (actual value 2999247013,972682)
2999247013,97268 // comments: (actual value 2999247013,97268)
2999247013,972682 // comments: (actual value 2999247013,972682)
(‘,’ is the decimal separator on current locale)
What i am trying to achieve is to use a Convert.ToDecimal(d) and get the decimal number 2999247013,972682.
Any thoughts anyone?
It’s because the
Decimalvalue returned can only have a maximum of 15 signification digits. From MSDN.Therefore, if there are more than 15 significant digits it will be rounded, which is what is happening in your case since the
doublevalue you’re passing has 16 significant digits.