Efficient BigDecimal round Up and down to two decimals

后端 未结 3 468
渐次进展
渐次进展 2020-12-10 14:17

In java am trying to find an efficient way to round a BigDecimal to two decimals, Up or Down based on a condition.

 IF condition true then:
    12.390 --->         


        
3条回答
  •  [愿得一人]
    2020-12-10 14:45

    I suggest the following (standing on the shoulders of giants...):

    public BigDecimal roundNumber(final BigDecimal number, final boolean isFloor){
         return number.setScale(2, isFloor ? RoundingMode.FLOOR 
                                           : RoundingMode.CEILING);
    }
    

提交回复
热议问题