Java Math round() 方法

定义和用法

round() 方法将数字四舍五入到最接近的整数。

实例

将数字四舍五入到最接近的整数:

System.out.println(Math.round(0.60));
System.out.println(Math.round(0.40));
System.out.println(Math.round(5));
System.out.println(Math.round(5.1));
System.out.println(Math.round(-5.1));
System.out.println(Math.round(-5.9));

亲自试一试

语法

以下之一:

public static long round(double number)
public static int round(float number)

参数

参数 描述
number 必需。要四舍五入的数字。

技术细节

返回: 表示最接近数字的整数的 long 值(如果参数为 double)或 int 值(如果参数为 float)。
Java 版本: 任意版本。