在 java 中将浮点值截断到小数点后两位
Truncate Float value up to two decimal point in java
DecimalFormat hisFormat = new DecimalFormat("########.##");
hisFormat.setRoundingMode(RoundingMode.DOWN);
Float x=12345678.12345f;
System.out.println("Float Value " + hisFormat.format(x));
以上代码打印 "Float Value" 为 12345678
我需要 12345678.12
我怎样才能得到我的结果?请告诉我。
使用 Double
而不是 Float
,否则你会失去精度
DecimalFormat hisFormat = new DecimalFormat("######.##");
hisFormat.setRoundingMode(RoundingMode.DOWN);
Double x = 12345678.12345;
System.out.println("Float Value " + hisFormat.format(x));
使用Float
结果为12345678
使用Double
结果为12345678.12
DecimalFormat hisFormat = new DecimalFormat("########.##");
hisFormat.setRoundingMode(RoundingMode.DOWN);
Float x=12345678.12345f;
System.out.println("Float Value " + hisFormat.format(x));
以上代码打印 "Float Value" 为 12345678 我需要 12345678.12
我怎样才能得到我的结果?请告诉我。
使用 Double
而不是 Float
,否则你会失去精度
DecimalFormat hisFormat = new DecimalFormat("######.##");
hisFormat.setRoundingMode(RoundingMode.DOWN);
Double x = 12345678.12345;
System.out.println("Float Value " + hisFormat.format(x));
使用Float
结果为12345678
使用Double
结果为12345678.12