Java BigDecimal.round()
Java BigDecimal.round()
我正在尝试使用以下代码将 BigDecimal 值四舍五入到最接近的 1000
BigDecimal oldValue = new BigDecimal("791232");
BigDecimal newValue=oldValue.round(new MathContext(3,
RoundingMode.UP));
System.out.println("Old ------- "+oldValue.intValue());
System.out.println("New------- "+newValue.intValue());
这对上面的输入很好用。结果如下
Old------>791232
New------>792000
但代码不适用于输入
< 1,00,000(例如 79123)并且输入 > 10,00,000(例如 7912354)
还有一点注意到,如果我们将精度从 3 更改为 2,如下所示
new MathContext(2,RoundingMode.UP)
然后它适用于输入 < 1,00,000。
请帮忙
您要对精度进行四舍五入的操作,例如
1.2345四舍五入到3位精度为1.23
如果你想四舍五入到最接近的1000,你可以这样做;
- 除以 1000
- 四舍五入到最接近的整数
- 乘以 1000
不需要 divide/multiply 1000,你只是忘了 set the scale
oldValue.setScale(0, RoundingMode.UP);
之后代码就如您所愿地工作了:
BigDecimal oldValue = new BigDecimal("79123");
oldValue = oldValue.setScale(0, RoundingMode.UP);
BigDecimal newValue = oldValue.round(new MathContext(3, RoundingMode.UP));
System.out.println("Old ------- " + oldValue.intValue());
System.out.println("New------- " + newValue.intValue());
输出:
Old ------- 79123
New------- 79200
和
Old ------- 7912113
New------- 7920000
您也可以更新您的代码:
BigDecimal newValue = oldValue.round(new MathContext(oldValue.precision() - 3,
RoundingMode.CEILING));
如果你想四舍五入到最近的 1000,你不应该使用RoundingMode.UP
,它是
Rounding mode to round away from zero.
您想使用 RoundingMode.HALF_UP
即:
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
此外,我建议使用 ΦXocę 웃 Пepeúpa ツ 建议的比例参数。
MathContext
精度属性是要使用的位数(即有效数字的个数)。要舍入到特定的小数位数,请使用带正数的 setScale
;如果你想四舍五入到 10 的幂,请使用负数 - 在你的情况下,使用 -3 来四舍五入到 1000。另外,正如 Rumid 所说的那样,如果你想四舍五入到最近的 1000(不是 up 到下一个 1000)然后使用 RoundingMode.HALF_UP(或者 RoundingMode.HALF_EVEN,如果你愿意的话):
BigDecimal b = new BigDecimal("7725232");
b = b.setScale(-3, RoundingMode.HALF_UP);
System.out.println(b.intValue());
您的精度决定了您号码左侧的位数。因此,要解决此问题,请取总长度并减去您的预期精度。
结果代码是:
BigDecimal oldValue = new BigDecimal("77252");
int expectedPrecision = 3;
int length = oldValue.precision() - expectedPrecision;
BigDecimal newValue=oldValue.round(new MathContext(length, RoundingMode.UP));
System.out.println("Old ------- "+oldValue.intValue());
System.out.println("New------- "+newValue.intValue());
我正在尝试使用以下代码将 BigDecimal 值四舍五入到最接近的 1000
BigDecimal oldValue = new BigDecimal("791232");
BigDecimal newValue=oldValue.round(new MathContext(3,
RoundingMode.UP));
System.out.println("Old ------- "+oldValue.intValue());
System.out.println("New------- "+newValue.intValue());
这对上面的输入很好用。结果如下
Old------>791232
New------>792000
但代码不适用于输入 < 1,00,000(例如 79123)并且输入 > 10,00,000(例如 7912354)
还有一点注意到,如果我们将精度从 3 更改为 2,如下所示
new MathContext(2,RoundingMode.UP)
然后它适用于输入 < 1,00,000。
请帮忙
您要对精度进行四舍五入的操作,例如
1.2345四舍五入到3位精度为1.23
如果你想四舍五入到最接近的1000,你可以这样做;
- 除以 1000
- 四舍五入到最接近的整数
- 乘以 1000
不需要 divide/multiply 1000,你只是忘了 set the scale
oldValue.setScale(0, RoundingMode.UP);
之后代码就如您所愿地工作了:
BigDecimal oldValue = new BigDecimal("79123");
oldValue = oldValue.setScale(0, RoundingMode.UP);
BigDecimal newValue = oldValue.round(new MathContext(3, RoundingMode.UP));
System.out.println("Old ------- " + oldValue.intValue());
System.out.println("New------- " + newValue.intValue());
输出:
Old ------- 79123
New------- 79200
和
Old ------- 7912113
New------- 7920000
您也可以更新您的代码:
BigDecimal newValue = oldValue.round(new MathContext(oldValue.precision() - 3,
RoundingMode.CEILING));
如果你想四舍五入到最近的 1000,你不应该使用RoundingMode.UP
,它是
Rounding mode to round away from zero.
您想使用 RoundingMode.HALF_UP
即:
Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
此外,我建议使用 ΦXocę 웃 Пepeúpa ツ 建议的比例参数。
MathContext
精度属性是要使用的位数(即有效数字的个数)。要舍入到特定的小数位数,请使用带正数的 setScale
;如果你想四舍五入到 10 的幂,请使用负数 - 在你的情况下,使用 -3 来四舍五入到 1000。另外,正如 Rumid 所说的那样,如果你想四舍五入到最近的 1000(不是 up 到下一个 1000)然后使用 RoundingMode.HALF_UP(或者 RoundingMode.HALF_EVEN,如果你愿意的话):
BigDecimal b = new BigDecimal("7725232");
b = b.setScale(-3, RoundingMode.HALF_UP);
System.out.println(b.intValue());
您的精度决定了您号码左侧的位数。因此,要解决此问题,请取总长度并减去您的预期精度。
结果代码是:
BigDecimal oldValue = new BigDecimal("77252");
int expectedPrecision = 3;
int length = oldValue.precision() - expectedPrecision;
BigDecimal newValue=oldValue.round(new MathContext(length, RoundingMode.UP));
System.out.println("Old ------- "+oldValue.intValue());
System.out.println("New------- "+newValue.intValue());