Math.round()方法

来源:互联网 发布:python spark sql 编辑:程序博客网 时间:2024/05/18 00:42

Math.round()即四舍五入,看如下代码:

public class MathTest {    public static void main(String[] args) {        System.out.println("小数点后第一位=5");        System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));        System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));        System.out.println();        System.out.println("小数点后第一位<5");        System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));        System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));        System.out.println();        System.out.println("小数点后第一位>5");        System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));        System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));        }}

输出结果为:

小数点后第一位=5
正数:Math.round(11.5)=12
负数:Math.round(-11.5)=-11

小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11

小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12

结论:

1、参数的小数点后第一位<5,运算结果为参数整数部分。

2、参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。

3、参数的 小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。

总结: 大于五全部加,等于五正数加, 小于五全不加。

0 0