相关文章推荐
玩篮球的煎鸡蛋  ·  SqlServer ...·  2 年前    · 
行走的酱肘子  ·  QT ...·  2 年前    · 
强悍的松鼠  ·  Range.Insert 方法 ...·  2 年前    · 
无聊的豆浆  ·  jquery ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
                One is a RoundingMode and one is an int field of a BigDecimal - the error tells you that. Is there something more to your question?
– Boris the Spider
                Nov 2, 2013 at 22:49
                Why is one RoundingMode and the other int? What is the difference between the two? docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html doesn't really explain anything
– user2948708
                Nov 2, 2013 at 22:50
                Because MathContext is part of the java math API, as is RoundingMode. The ROUND_HALF_UP field of BigDecimal is part of BigDecimal's own implementation detail.
– Boris the Spider
                Nov 2, 2013 at 22:55
                Why not look at the code? Google is your friend. RoundingMode is an enum. And in fact one quick glance at the code tells you that RoundingMode.valueOf(BigDecimal.ROUND_HALF_UP) == RoundingMode.HALF_UP. And in fact the comment in the code: Returns the RoundingMode object corresponding to the legacy rounding modes (emphasis mine).
– Boris the Spider
                Nov 2, 2013 at 23:06
                Enums are classes in Java. Not ints. Google for Java enum tutorial, and read it. If a variable has the type RoundingMode, then its type is RoundingMode, and not int.
– JB Nizet
                Nov 2, 2013 at 23:12

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

public enum RoundingMode {
HALF_UP(BigDecimal.ROUND_HALF_UP),

Please use BigDecimal.ROUND_HALF_UP instead of RoundingMode.HALF_UP because of RoundingMode.HALF_UP is calling BigDecimal.ROUND_HALF_UP internally so both will give you same result but RoundingMode.HALF_UP will require one more step.

Sources from java doc:

BigDecimal.ROUND_HALF_UP

public static final RoundingMode HALF_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. (click here to know more)

RoundingMode.HALF_UP

public final static int ROUND_HALF_UP Behave as for ROUND_UP if the discarded fraction is >= .5; otherwise, behave as for ROUND_DOWN. (Rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case rounds up.) (click here to know more)

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.