# Math.round()函数

149人阅读 评论(0)

Math.round()可以简单的理解为四舍五入函数，在负数的情况下0.5不进位。

public class TestDemo {

public static void main(String[] args) {
long r1=Math.round(11.1);
long r2=Math.round(11.5);
long r3=Math.round(11.6);
long r4=Math.round(-11.1);
long r5=Math.round(-11.5);
long r6=Math.round(-11.6);

System.out.println("11.1->"+r1);
System.out.println("11.5->"+r2);
System.out.println("11.6->"+r3);
System.out.println("-11.1->"+r4);
System.out.println("-11.5->"+r5);
System.out.println("-11.6->"+r6);

}

}


Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to typelong. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)

Special cases:

• If the argument is NaN, the result is 0.
• If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
• If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.
Parameters:
a a floating-point value to be rounded to a long.
Returns:

the value of the argument rounded to the nearest long value.

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：5568次
• 积分：491
• 等级：
• 排名：千里之外
• 原创：43篇
• 转载：4篇
• 译文：0篇
• 评论：0条
文章分类