保留最多 3 位小数而不进行任何舍入
Keep up to 3 decimals without doing any rounding
我有一个双精度数,我只想保留小数点后 3 位,但 根本没有 应用任何舍入。
例如。 92.36699
应该是 92.366
我尝试了以下方法:
DecimalFormat nf= new DecimalFormat("#0.000");
String number = nf.format(originalNumber);
但这会导致 92.367
我怎样才能做我需要的?
这不是 "no rounding",是 DOWN
rounding. Simply 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”。
我有一个双精度数,我只想保留小数点后 3 位,但 根本没有 应用任何舍入。
例如。 92.36699
应该是 92.366
我尝试了以下方法:
DecimalFormat nf= new DecimalFormat("#0.000");
String number = nf.format(originalNumber);
但这会导致 92.367
我怎样才能做我需要的?
这不是 "no rounding",是 DOWN
rounding. Simply 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”。