BigDecimal 方法发送格式化数字
BigDecimal methods send formatted numbers
我正在尝试将我的大 BigDecimals 四舍五入到小数点后三位。例如,假设我有一些
BigDecimal X = 1362.59633
我想得到:
1362.596
这是我正在做的事情:
BigDecimal Y = X.round(new MathContext(3));
但我一直得到这个:1.36E+3。
在这种情况下我该怎么办?
谢谢
实际上你不必四舍五入,但你必须设置 BigDecimal
:
的比例
BigDecimal X = new BigDecimal("1362.59633");
X = X.setScale(3, BigDecimal.ROUND_HALF_UP);
System.out.println(X.doubleValue());
这将打印出 1362.596
。
注意 setScale
returns 一个新的 BigDecimal
所以你必须像我在我的例子中那样分配它。
编辑:有几种舍入策略。检查 BigDecimal
class 以供参考。我编辑了我的答案以使用 BigDecimal.ROUND_HALF_UP
.
BigDecimal y= x.round(new MathContext(7, RoundingMode.HALF_EVEN));
这应该适合你。
如果你只使用double
你可以做到
double d = 1362.59633;
double d2 = Math.round(d * 1e3) / 1e3;
System.out.println(d2);
打印
1362.596
注意:这不会创建任何对象来执行舍入。
BigDecimal X = new BigDecimal(1362.59633);
X = X.setScale(3,RoundingMode.HALF_EVEN);
System.out.println(X); //1362.596
Note that since BigDecimal objects are immutable, calls of this method
do not result in the original object being modified, contrary to the
usual convention of having methods named setX mutate field X. Instead,
setScale returns an object with the proper scale; the returned object
may or may not be newly allocated.
我们可以使用DecimalFormat class来定义double数据的格式。您可以定义 new DecimalFormat("####.000")
我们可以调用你想要的值的格式化方法。
例子
DecimalFormat format = new DecimalFormat("####.000");
format.format("1362.59633");
- 首先将比例设置为 3,使用
setScale
with RoundingMode.DOWN
作为截断的舍入模式
- 然后调用
toPlainString
来保证return没有指数字段的字符串表示
- 在
BigDecimal
上调用 toString
并不能保证没有指数的 String
的 return。如果需要指数,它return是使用科学记数法的字符串表示
基于您的问题的示例:
BigDecimal x = BigDecimal.valueOf( 1362.59633 );
System.out.println( x.setScale( 3, RoundingMode.DOWN ).toPlainString( ) );
我正在尝试将我的大 BigDecimals 四舍五入到小数点后三位。例如,假设我有一些
BigDecimal X = 1362.59633
我想得到:
1362.596
这是我正在做的事情:
BigDecimal Y = X.round(new MathContext(3));
但我一直得到这个:1.36E+3。 在这种情况下我该怎么办? 谢谢
实际上你不必四舍五入,但你必须设置 BigDecimal
:
BigDecimal X = new BigDecimal("1362.59633");
X = X.setScale(3, BigDecimal.ROUND_HALF_UP);
System.out.println(X.doubleValue());
这将打印出 1362.596
。
注意 setScale
returns 一个新的 BigDecimal
所以你必须像我在我的例子中那样分配它。
编辑:有几种舍入策略。检查 BigDecimal
class 以供参考。我编辑了我的答案以使用 BigDecimal.ROUND_HALF_UP
.
BigDecimal y= x.round(new MathContext(7, RoundingMode.HALF_EVEN));
这应该适合你。
如果你只使用double
你可以做到
double d = 1362.59633;
double d2 = Math.round(d * 1e3) / 1e3;
System.out.println(d2);
打印
1362.596
注意:这不会创建任何对象来执行舍入。
BigDecimal X = new BigDecimal(1362.59633);
X = X.setScale(3,RoundingMode.HALF_EVEN);
System.out.println(X); //1362.596
Note that since BigDecimal objects are immutable, calls of this method do not result in the original object being modified, contrary to the usual convention of having methods named setX mutate field X. Instead, setScale returns an object with the proper scale; the returned object may or may not be newly allocated.
我们可以使用DecimalFormat class来定义double数据的格式。您可以定义 new DecimalFormat("####.000")
我们可以调用你想要的值的格式化方法。
例子 DecimalFormat format = new DecimalFormat("####.000"); format.format("1362.59633");
- 首先将比例设置为 3,使用
setScale
withRoundingMode.DOWN
作为截断的舍入模式 - 然后调用
toPlainString
来保证return没有指数字段的字符串表示 - 在
BigDecimal
上调用toString
并不能保证没有指数的String
的 return。如果需要指数,它return是使用科学记数法的字符串表示
基于您的问题的示例:
BigDecimal x = BigDecimal.valueOf( 1362.59633 );
System.out.println( x.setScale( 3, RoundingMode.DOWN ).toPlainString( ) );