现在的位置: 首页 > 综合 > 正文

Math.round(doubleValue)=【加半地板造型long】

2013年09月07日 ⁄ 综合 ⁄ 共 649字 ⁄ 字号 评论关闭

好久没有使用Math类,记得不清楚了,以至于看了个SCJP的题都能做错,今天给自己的记忆refresh一下,牢记!

public class T {
	public static void main(String[] args) {
		System.out.println(Math.round(-4.1));
		System.out.println(Math.round(-4.8));
		System.out.println("---");
		System.out.println(Math.ceil(-4.8));
		System.out.println(Math.floor(-4.8));
	}
}

 这段代码的输出:

-4
-5
---
-4.0
-5.0

关于ceil和floor:

1.ceil是天花板,就是往上往大,比如2.1和2.9都要够到3;-2.1和-2.9都要够到-2;因为要够大的。

2.floor是地板,就是往下往小,比如2.1和2.9->2;-2.1和-2.9->-3;因为要够到小;

3.返回值与原值类型一样,还是double;

----------------------------------------死皮赖脸的分割线-------------------------------------------

关于Math.round(Double a)要记住的几点:

1.--double返回long;float返回int;

2.--四舍五入的计算公式是参数(long)Math.floor(a+0.5d)--就是加半地板造型long

抱歉!评论已关闭.