Java 将 BigDecimal 向上舍入为整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26102362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Round up BigDecimal to Integer value
提问by Rodrigo Martinez
I have a BigDecimal which value is 450.90, I want to round up to next hole integer value, and then print the Integer value without any decimal points, like this;
我有一个 BigDecimal,其值为 450.90,我想向上取整到下一个孔整数值,然后打印没有任何小数点的整数值,如下所示;
Val: 450.90 -> Rounded: 451.00 -> Output: 451
Val:450.90 -> 圆角:451.00 -> 输出:451
Val: 100.00001 -> Rounded: 101.00000 Output: 101
Val: 100.00001 -> 四舍五入: 101.00000 输出: 101
Checked some solutions but I'm not getting the expected result, heres my code;
检查了一些解决方案,但我没有得到预期的结果,这是我的代码;
BigDecimal value = new BigDecimal(450.90);
value.setScale(0, RoundingMode.HALF_UP); //Also tried with RoundingMode.UP
return value.intValue();
Thanks!
谢谢!
采纳答案by T.J. Crowder
setScale
returnsa new BigDecimal
with the result, it doesn't change the instance you call it on. So assign the return value back to value
:
setScale
返回一个新BigDecimal
的结果,它不会改变你调用它的实例。因此将返回值分配回value
:
value = value.setScale(0, RoundingMode.UP);
I also changed it to RoundingMode.UP
because you said you alwayswanted to round up. But depending on your needs, you might want RoundingMode.CEILING
instead; it depends on what you want -451.2
to become (-452
[UP
] or -451
[CEILING
]). See RoundingMode
for more.
我也把它改成RoundingMode.UP
因为你说你一直想围捕。但根据您的需要,您可能想要RoundingMode.CEILING
;这取决于你想-451.2
成为什么(-452
[ UP
]或-451
[ CEILING
])。查看RoundingMode
更多。
回答by venergiac
you need to use the returned value
您需要使用返回值
BigDecimal value = new BigDecimal(450.90);
value = value.setScale(0, RoundingMode.HALF_UP); //Also tried with RoundingMode.UP
note that BigDecimalis invariant
注意BigDecimal是不变的
Scaling/rounding operations (setScale and round) return a BigDecimal whose value is approximately (or exactly) equal to that of the operand, but whose scale or precision is the specified value; that is, they increase or decrease the precision of the stored number with minimal effect on its value.
缩放/舍入操作(setScale 和 round)返回一个 BigDecimal,其值大约(或完全)等于操作数的值,但其比例或精度是指定值;也就是说,它们增加或减少存储数字的精度,而对其值的影响最小。
回答by przemek hertel
use:
用:
value.setScale(0, RoundingMode.CEILING);
回答by starvator
回答by Fire
From method description:
从方法描述:
Note that since BigDecimal objects are immutable, calls of setScale 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.
请注意,由于 BigDecimal 对象是不可变的,因此调用 setScale 方法不会导致原始对象被修改,这与将方法命名为 setX mutate field X 的通常约定相反。相反,setScale 返回具有适当比例的对象;返回的对象可能是也可能不是新分配的。
public void test() {
BigDecimal value = new BigDecimal(450.90);
System.out.println(value.setScale(0, RoundingMode.HALF_UP)); // prints 451
}