Why does Double.Parse(Double.MaxValue.ToString()) not work? [Anthony Moore]

Why does Double.Parse(Double.MaxValue.ToString()) not work? [Anthony Moore]

  • Comments 1

System.Double and System.Single are have inexact precision of decimal digits because internally they are a floating point binary number. As such, beyond a certain number of significant digits, their decimal representation becomes approximate. The standard Double.ToString() and Single.ToString() thus are not guaranteed to round-trip back to the same value if you call Parse or TryParse.

There is a formatting option “r”, such that Double.ToString(“r”) will output a decimal representation that will round-trip back to the same value, which may be what is required. This call is slower because of the extra work it needs to do to account for the approximations that get made while parsing.

The reason Double cannot parse its minimum and maximum values, but Single can is because Double round in a direction such that the maximum and minimum values round-trip back to a number slightly further from 0, and thus one that will not fit in a Double. Single happens to round the other way.

Page 1 of 1 (1 items)