gpt4 book ai didi

java - BigDecimal.ROUND_HALF_UP 和 RoundingMode.HALF_UP 之间的区别?

转载 作者:搜寻专家 更新时间:2023-11-01 03:39:08 32 4
gpt4 key购买 nike

以下:

new MathContext(precision, RoundingMode.HALF_UP);

似乎有效。但是,以下返回错误:

new MathContext(precision, BigDecimal.ROUND_HALF_UP);

错误:

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)

最佳答案

请注意常量:

RoundingMode.HALF_UP
BigDecimal.ROUND_HALF_UP

根据 Javadocs 和源代码,意思完全相同:

public enum RoundingMode {
....
HALF_UP(BigDecimal.ROUND_HALF_UP),
....
}

关于java - BigDecimal.ROUND_HALF_UP 和 RoundingMode.HALF_UP 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19747652/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com