losing precision converting from java BigDecimal to double

后端 未结 5 1476
遥遥无期
遥遥无期 2020-12-15 08:56

I am working with an application that is based entirely on doubles, and am having trouble in one utility method that parses a string into a double. I\'ve found a fix where

5条回答
  •  死守一世寂寞
    2020-12-15 09:47

    Only that many digits are printed so that, when parsing the string back to double, it will result in the exact same value.

    Some detail can be found in the javadoc for Double#toString

    How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0.

提交回复
热议问题