Math.round()和Math.floor()

Math.round()为四舍五入,但是负数则会舍弃-5。

System.out.println(Math.round(2.6));//3
System.out.println(Math.round(-1.6));//-2
System.out.println(Math.round(-1.5));//-1

Math.floor()为直接去掉小数保留整数,如果参数为正数则小数全舍,参数为负数则全入。

System.out.println(Math.floor(2.6));//2
System.out.println(Math.floor(-2.3));//-3

猜你喜欢

转载自blog.csdn.net/weixin_44168355/article/details/90105865