java round函数怎么用

Python012

java round函数怎么用,第1张

用于 小数Math.Round() ;函数 就是 去四舍五入 。

Math.Round(1.3) 结果为 1 ;Math.Round(1.6) 结果为 2 ;

多看看 Math 吧

java四舍五入的函数:Math.round

语法:

Math.round(x)

参数:

x 为一数值。

解释:

方法。返回对参数x四舍五入后所得的整数近似值。

例子:

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))

}

}

运行结果:

1、小数点后第一位=5

2、正数:Math.round(11.5)=12

3、负数:Math.round(-11.5)=-11

4、

5、小数点后第一位<5

6、正数:Math.round(11.46)=11

7、负数:Math.round(-11.46)=-11

8、

9、小数点后第一位>5

10、正数:Math.round(11.68)=12

11、负数:Math.round(-11.68)=-12