简体   繁体   English

2 位小数的 BigDecimal 舍入模式

[英]BigDecimal rounding mode for 2 decimal

Using BigDecimal what is the mode I should use for the below condition to round up to decimal point?使用 BigDecimal 对于以下条件,我应该使用什么模式来四舍五入到小数点?

If >= USD 0.005 rounds up to USD 0.01, If the amount is < USD 0.005 round down to 0.00.如果 >= 0.005 美元向上舍入到 0.01 美元,如果金额小于 0.005 美元,向下舍入到 0.00。

Should I use RoundingMode.HALF_UP ?我应该使用RoundingMode.HALF_UP吗?

I assume you want to round < 0.005 to 0.00 and > 0.005 to 0.01.我假设您想将 < 0.005 舍入到 0.00 并将 > 0.005 舍入到 0.01。

 BigDecimal bd = new BigDecimal(0.004999);
 bd =  bd.setScale(2, BigDecimal.ROUND_HALF_UP);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM