在Java中,Math.round()
函数用于将一个浮点数四舍五入为最接近的整数。
Math.round()
函数有两种用法:
long
类型的整数。double number = 3.5;
long roundedNumber = Math.round(number);
System.out.println(roundedNumber); // 输出结果为 4
int
类型的整数。double number = 3.5;
int roundedNumber = Math.round(number);
System.out.println(roundedNumber); // 输出结果为 4
注意:Math.round()
函数的参数可以是float
类型或double
类型的浮点数。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>