Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a double string is round-trip safe?

I have a text representation of a double and want to know if it's safe to round-trip it to double and back. How do I know this if I also want to accept any kind of number-style of the input? Or how do I know if any precision is lost when a double-string is parsed with Double.Parse? Or how do I ToString a double to match the same format as another double-string? An answer to any of these questions would be a solution I think.

like image 992
Andreas Zita Avatar asked Oct 07 '22 20:10

Andreas Zita


1 Answers

Use the R format specifier to convert the double to a string:

myDouble.ToString("R")

See The Round-trip ("R") Format Specifier on MSDN.

The round-trip ("R") format specifier guarantees that a numeric value that is converted to a string will be parsed back into the same numeric value. This format is supported only for the Single, Double, and BigInteger types.

(emphasis mine)

like image 127
Oded Avatar answered Oct 10 '22 03:10

Oded