我有一个 double ,我只想保留 3 个小数位,但不应用任何舍入。
例如。 92.36699
应为 92.366
我尝试了以下方法:
DecimalFormat nf= new DecimalFormat("#0.000");
String number = nf.format(originalNumber);
但这会导致92.367
我怎样才能做我需要的事情?
最佳答案
这不是“无舍入”,而是 DOWN
rounding 。简单set the roundingMode .
DecimalFormat nf = new DecimalFormat("#0.000");
nf.setRoundingMode(RoundingMode.DOWN);
String number = nf.format(originalNumber);
请注意 FLOOR
和 DOWN
之间的区别 - 仅与负数相关。 FLOOR
向负无穷大舍入,因此 -92.36699
将变为“-92.367”。
关于java - 不进行任何舍入,最多保留 3 位小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41999696/