Java Math.round() 方法:浮点数四舍五入
Math.round() 方法是 Java 中的一个数学函数,用于将一个浮点数四舍五入为最接近的整数。
使用方法:
Math.round(double num)
参数:
num:要进行四舍五入的浮点数。
返回值:
- 一个
long类型的整数,表示四舍五入后的结果。
示例代码:
double num = 3.14159;
long roundedNum = Math.round(num);
System.out.println(roundedNum); // 输出结果为 3
在上述示例中,将浮点数 3.14159 四舍五入为最接近的整数,结果为 3。
原文地址: https://www.cveoy.top/t/topic/hMyG 著作权归作者所有。请勿转载和采集!