use*_*708 5 java rounding bigdecimal
下面:
new MathContext(precision, RoundingMode.HALF_UP);
Run Code Online (Sandbox Code Playgroud)
似乎工作。但是,以下返回错误:
new MathContext(precision, BigDecimal.ROUND_HALF_UP);
Run Code Online (Sandbox Code Playgroud)
错误:
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)
Run Code Online (Sandbox Code Playgroud)
小智 3
请注意常数:
RoundingMode.HALF_UP
BigDecimal.ROUND_HALF_UP
Run Code Online (Sandbox Code Playgroud)
根据 Javadocs 和源代码,含义完全相同:
public enum RoundingMode {
....
HALF_UP(BigDecimal.ROUND_HALF_UP),
....
}
Run Code Online (Sandbox Code Playgroud)