有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java精度为双精度。parseDouble()和字符串。价值()

以下语句是否适用于除NaN之外的任何double(Java原语双精度IEEE-754):

Double.parseDouble(String.valueOf(d)) == d

否则,解析序列化的(使用String.valueOf()的)双精度值是否总是产生准确的原始双精度值


共 (1) 个答案

  1. # 1 楼答案

    除了你说过的NaN之外,是的,这个不变量应该成立。如果不是,那就是JDK的错误

    Double.toString在其Javadoc中说:

    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.

    总之,它返回足够的数字来唯一地标识这个双精度,因此Double.parseDouble应该返回转换为字符串的完全相同的双精度