Convert a Double with more than 13 decimal places to String without losing values

Asked

Viewed 59 times

3

I have in C# the following number in Double: 124.00767638369544. When I simply put valor.toString(), the value goes to: 124.007676383695, IGNORING the 0,00000000000044. The system I’m making needs that precision.

I’ve tried to force region, parse, and a multitude of different conversions and nothing.
Anyone have any suggestions? I believe that follow the limitation of Excel, that... if you put the same value in a cell, is also disregarded the 0,00000000000044.

1 answer

2

It is rounding to an accuracy of 15 digits, which is the standard of ToString() for this type, according to the link below.

But the ToString() allows you to pass formatting parameters such as accuracy.

Like the guy Double has a higher precision, can use so to have all the digits:

double num = 124.00767638369544
String numString = num.ToString("G17");

The "G" format is a "General" number, and the 17 is the precision

You can also try the "R" format, which tries to convert to a value that can be converted again to the original type, in this case Double, no loss, but the documentation recommends using the "G17": https://docs.microsoft.com/pt-br/dotnet/standard/base-types/standard-numeric-format-strings?redirectedfrom=MSDN#RFormatString

More about the formats of ToString(): https://docs.microsoft.com/pt-BR/dotnet/standard/base-types/standard-numeric-format-strings

Browser other questions tagged

You are not signed in. Login or sign up in order to post.