java 浮动四舍五入到小数点后2位java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17577446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Float round up to 2 decimals java
提问by wvp
I'm trying to round a float up to 2 decimals.
我正在尝试将浮点数四舍五入到小数点后两位。
I've 2 float values:
我有 2 个浮点值:
1.985
29.294998
Both of them will need to be rounded up so I end up with the following:
它们都需要四舍五入,所以我最终得到以下结果:
1.99
29.30
When I use the following method:
当我使用以下方法时:
public static Float precision(int decimalPlace, Float d) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.floatValue();
}
Te result is:
结果是:
1.99
29.29
回答by Jonathan Drapeau
Since you are using BigDecimal.ROUND_HALF_UP
, 29.294998
is rounded to 29.29
. You might want to use BigDecimal.ROUND_UP
instead.
由于您使用的BigDecimal.ROUND_HALF_UP
,29.294998
四舍五入到29.29
。您可能想BigDecimal.ROUND_UP
改用。
Check BigDecimal docfor more informations on each rounding available.
检查BigDecimal 文档以获取有关每个可用舍入的更多信息。
回答by nachokk
Instead of this
而不是这个
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
Use
利用
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_CEILING);
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_CEILING);
About ROUND_CEILING
关于 ROUND_CEILING
Rounding mode to round towards positive infinity. If the BigDecimal is positive, behaves as for ROUND_UP; if negative, behaves as for ROUND_DOWN. Note that this rounding mode never decreases the calculated value.
舍入模式向正无穷大舍入。如果 BigDecimal 为正,则表现为 ROUND_UP;如果为负,则与 ROUND_DOWN 相同。请注意,此舍入模式永远不会减少计算值。
You can use DecimalFormat
if you want.
DecimalFormat
如果你愿意,你可以使用。
回答by Joachim Isaksson
Since you always want to round up, what you want is simply not BigDecimal.ROUND_HALF_UP
but instead BigDecimal.ROUND_UP
or BigDecimal.ROUND_CEILING
.
由于您总是想要四舍五入,因此您想要的只是 not BigDecimal.ROUND_HALF_UP
but instead BigDecimal.ROUND_UP
or BigDecimal.ROUND_CEILING
。
Use BigDecimal.ROUND_UP
if you want negativenumbers to round down (-29.294998 to -29.30).
Use BigDecimal.ROUND_CEILING
if you want negativenumbers to round up (-29.294998 to -29.29).
使用BigDecimal.ROUND_UP
,如果你想负到本轮下跌(-29.294998至-29.30)的数字。
使用BigDecimal.ROUND_CEILING
,如果你想负围捕(-29.294998至-29.29)的数字。
With positive numbers, they will both do the rounding you're trying to do (ie round up)
对于正数,它们都将进行您想要进行的四舍五入(即向上取整)
回答by StephenTG
You're using ROUND_HALF_UP
, when you should be using ROUND_UP
. ROUND_HALF_UP
rounds to the closest number with the given precision, rounding up to break ties (ie 1.235 rounds to 1.24)
您正在使用ROUND_HALF_UP
,而您应该使用ROUND_UP
. ROUND_HALF_UP
以给定的精度四舍五入到最接近的数字,四舍五入以打破平局(即 1.235 舍入到 1.24)
回答by velo
ROUND_HALP_UP
ROUND_HALP_UP
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is >= 0.5; otherwise, behaves as for RoundingMode.DOWN. Note that this is the rounding mode commonly taught at school.
舍入模式向“最近邻居”舍入,除非两个邻居等距,在这种情况下向上舍入。如果丢弃的分数 >= 0.5,则行为与 RoundingMode.UP 相同;否则,行为与 RoundingMode.DOWN 相同。请注意,这是学校通常教授的舍入模式。
One of the checks that is done is wether the magnitude of the remainder being dropped (2*4998) is greater than the divisor (10000). In this case it is not so its determined to be closer to the lower end. If you try 29.295001 that rounds to 29.3 for ROUND_HALP_UP .
进行的检查之一是被丢弃的余数的大小 (2*4998) 是否大于除数 (10000)。在这种情况下,它不是那么确定更接近下端。如果您尝试 29.295001,则 ROUND_HALP_UP 舍入为 29.3。