java 字面量 Math.floor() round() ceil()
int i = 1;把整数1赋值给int型变量i,整数1就是Java字面量,
同样,String s = "abc";中的abc也是字面量。
floor 返回不大于的最大整数 public static double floor(double a)
round 则是4舍5入的计算,入的时候是到大于它的整数 public static long round(double a)
round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。
ceil 则是不小于他的最小整数 public static double ceil(double a)
看例子
| Math.floor | Math.round | Math.ceil | |
| 1.4 | 1 | 1 | 2 |
| 1.5 | 1 | 2 | 2 |
| 1.6 | 1 | 2 | 2 |
| -1.4 | -2 | -1 | -1 |
| -1.5 | -2 | -1 | -1 |
| -1.6 | -2 | -2 | -1 |

浙公网安备 33010602011771号