Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between BigDecimal.ROUND_HALF_UP and RoundingMode.HALF_UP?

The below:

new MathContext(precision, RoundingMode.HALF_UP);

seems to work. However, the following returns an error:

new MathContext(precision, BigDecimal.ROUND_HALF_UP);

Error:

java: no suitable constructor found for MathContext(int,int)
    constructor java.math.MathContext.MathContext(java.lang.String) is not applicable
      (actual and formal argument lists differ in length)
    constructor java.math.MathContext.MathContext(int,java.math.RoundingMode) is not applicable
      (actual argument int cannot be converted to java.math.RoundingMode by method invocation conversion)
    constructor java.math.MathContext.MathContext(int) is not applicable
      (actual and formal argument lists differ in length)
like image 363
user2948708 Avatar asked Nov 02 '13 22:11

user2948708


1 Answers

Please note that the constants:

RoundingMode.HALF_UP
BigDecimal.ROUND_HALF_UP

mean absolutely the same according to Javadocs and according to source code:

public enum RoundingMode {
....
HALF_UP(BigDecimal.ROUND_HALF_UP),
....
} 
like image 63
Vladimir Petrovich Avatar answered Oct 19 '22 11:10

Vladimir Petrovich