Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting decimal precision, .net

These lines in C#

decimal a = 2m; decimal b = 2.0m; decimal c = 2.00000000m; decimal d = 2.000000000000000000000000000m;  Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); 

Generates this output:

2 2.0 2.00000000 2.000000000000000000000000000 

So I can see that creating a decimal variable from a literal allows me to control the precision.

  • Can I adjust the precision of decimal variables without using literals?
  • How can I create b from a? How can I create b from c?
like image 414
Amy B Avatar asked Jul 15 '09 17:07

Amy B


People also ask

How do you specify decimal precision in C#?

In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf().

How do you increase decimal precision?

The easiest way to control rounding, and achieve a desired decimal precision, is to multiply an expression by 1, followed by the number of decimal places of precision that you want in the result. For example, multiply by 1.0000 to ensure that a result is accurate to four decimal places.

How do you change the precision of a number?

To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).

Is C# decimal floating-point?

In C#, Decimal Struct class is used to represent a decimal floating-point number. The range of decimal numbers is +79,228,162,514,264,337,593,543,950,335 to -79,228,162,514,264,337,593,543,950,335.


2 Answers

Preserving trailing zeroes like this was introduced in .NET 1.1 for more strict conformance with the ECMA CLI specification.

There is some info on this on MSDN, e.g. here.

You can adjust the precision as follows:

  • Math.Round (or Ceiling, Floor etc) to decrease precision (b from c)

  • Multiply by 1.000... (with the number of decimals you want) to increase precision - e.g. multiply by 1.0M to get b from a.

like image 87
Joe Avatar answered Sep 19 '22 15:09

Joe


You are just seeing different representations of the exact same data. The precision of a decimal will be scaled to be as big as it needs to be (within reason).

From System.Decimal:

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a Decimal value is of the form, ((-296 to 296) / 10(0 to 28)), where -296-1 is equal to MinValue, and 296-1 is equal to MaxValue.

The scaling factor also preserves any trailing zeroes in a Decimal number. Trailing zeroes do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeroes can be revealed by the ToString method if an appropriate format string is applied.

like image 33
Andrew Hare Avatar answered Sep 16 '22 15:09

Andrew Hare