有 Java 编程相关的问题?

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

java是否指定了大浮点文本的舍入模式?

考虑下面的代码:

System.out.printf("%f%n", 123456796f);

这会在我的系统上打印123456800,因此该值被四舍五入。在这种情况下,是强制四舍五入,还是编译器也会决定四舍五入,从而产生123456792


共 (1) 个答案

  1. # 1 楼答案

    Java Language Specification, §3.10.2, Floating-Point Literals

    The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float and class Double of the package java.lang.

    ^{} Javadoc

    s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type float by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic [...]