public class RoundExample {
public static void main(String[] args) {
double num = 123.456789;
double roundedNum = Math.round(num * 100.0) / 100.0;
System.out.println("Original number: " + num);
System.out.println("Rounded number: " + roundedNum);
}
}
Math.round(num * 100.0)
:首先将数字乘以100,将其小数点向右移动两位。Math.round()
:对结果进行四舍五入取整。/ 100.0
:再将结果除以100,恢复小数点位置,得到保留两位小数的值。这样可以实现保留两位小数的效果。
上一篇:java 有序的map
下一篇:java数组合并
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站