Java BigDecimal: Round to the nearest whole value

后端 未结 6 752
滥情空心
滥情空心 2020-11-27 16:41

I need the following results

100.12 -> 100.00
100.44 -> 100.00
100.50 -> 101.00
100.75 -> 101.00

.round() or

6条回答
  •  佛祖请我去吃肉
    2020-11-27 17:26

    Simply look at:

    http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#ROUND_HALF_UP

    and:

    setScale(int precision, int roundingMode)
    

    Or if using Java 6, then

    http://download.oracle.com/javase/6/docs/api/java/math/RoundingMode.html#HALF_UP

    http://download.oracle.com/javase/6/docs/api/java/math/MathContext.html

    and either:

    setScale(int precision, RoundingMode mode);
    round(MathContext mc);
    

提交回复
热议问题