1

how to round the below values to remove the consecutive 9's from a double value. Significant digit rounding in .Net framework and .net core working differently.

Used the below code to round the double value to significant digits by passing the value 5.9E-12 and rounding to 12 significant digits

  private static double RoundToSignificantDigits(double d, int digits)
         {
             if (d == 0)
                 return 0;

              double scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(d))) + 1);
              return scale * Math.Round(d / scale, digits);
         }

.Net core result : 5.8999999999999995E-12 .Net Framework Result : 5.9E-12

How to get the result like .Net framework in .Net core

Ramakrishna Reddy
  • 347
  • 1
  • 2
  • 18

1 Answers1

0

It is still unclear what exactly you want to achive. But you cannot represent any decimal number correctly as a double value.

Say you have a number x, it needs to hold to the following formula

x = n/m where n and m do not share any factor and m is a power of 2

to have a chance of being currectly represetable as double.

So the number 5.9E-12 is equal to 59/(10^13). You clearly see that 10^13 is not a power of 2 so 5.9E-12 can not corrctly represented as a double value

If you would do Console.WriteLine("{0:G17}", double.Parse("5.9E-12")); you would get the value 5.9000000000000003E-12 and not 5.9E-12.

All this basicly araises for the same reason you cant write the decimal expansion of 1/3 or 1/7 to the end (without the periodical shortcut).

So if you want numbers that can be rounded to values that have a finite decimal expansion use decimal and not double.

Or you could create a Fractional Type based on BitInteger that would have an unlimited amount of precision. See this for a discussion.

Ackdari
  • 3,222
  • 1
  • 16
  • 33