有 Java 编程相关的问题?

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

java与数学的混淆。第(轮)

以下是一些我感到困惑的代码:

public class MathOpration {  
   public static void main(String args[])  
   {  
       float F1 = -5.5f;   
float F2 = 5.5f;   
float F3 = -5.49f;   
float F4 = 5.49f;   
System.out.println("Round F1 is " + Math.round(F1));   
System.out.println("Round F2 is " + Math.round(F2));   
System.out.println("Round F3 is " + Math.round(F3));   
System.out.println("Round F4 is " + Math.round(F4));   

   }  
}  

上述结果是:

Round F1 is -5
Round F2 is 6
Round F3 is -5
Round F4 is 5 

我的困惑是-5是如何成为F1的输出的

任何帮助都将不胜感激


共 (3) 个答案

  1. # 1 楼答案

    -6 is less than -5
    

    当你围拢过来,你就上去了

  2. # 3 楼答案

    请阅读^{}的文档:

    In other words, the result is equal to the value of the expression:

    (int)Math.floor(a + 0.5f)
    

    这就解释了为什么-5.5f轮到-5,而5.5f轮到6。(^{}给出“返回不大于参数且等于数学整数的最大(最接近正无穷大)双精度值”。)