Converting Double Values into Decimal Values in C# Programming Language

For your consideration, I have two solutions to share. The first solution is a C++ wrapper for the Intel Decimal Floating-Point Math Library, which I believe may pique your interest. It is a wrapper class implementation. The second solution involves using the “Round” function to round your decimal to a specific number of decimal places.


Solution:

The exact value of

dbl

is 0.1000000000000000055511151231257827021181583404541015625.

The range is from 0.1 up to 17 digits that are significant.

The information provided in the documentation of

Convert.ToDecimal(Double)

mentions:

The method’s returned Decimal value has a limit of 15 significant digits and will be rounded to the nearest value if the value parameter exceeds this limit.

The documentation specifies that truncation occurs earlier during the conversion process of

Single

(also known as

float

).

The output of this method is a Decimal value with a precision of up to seven significant digits. In cases where the input parameter holds more than seven significant digits, the method will round it using the rounding to nearest method.

When you pass a value that was originally converted from 0.1f to

Convert.ToDecimal(Double)

, the displayed result will be 0.100000001490116.

double dbl = 0.1f;        
decimal dcml = (decimal) dbl;
Console.WriteLine(dcml);

Frequently Asked Questions

Posted in Uncategorized