简体   繁体   English

将BigDecimal向上舍入为整数值

[英]Round up BigDecimal to Integer value

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 - > Rounded:451.00 - >输出:451

Val: 100.00001 -> Rounded: 101.00000 Output: 101 Val:100.00001 - > Rounded: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! 谢谢!

setScale returns a new BigDecimal with the result, it doesn't change the instance you call it on. setScale 返回带有结果的新BigDecimal ,它不会更改您调用它的实例。 So assign the return value back to value : 因此,将返回值赋值回value

value = value.setScale(0, RoundingMode.UP);

Live Example 实例

I also changed it to RoundingMode.UP because you said you always wanted to round up. 我也把它改成了RoundingMode.UP因为你说你总是想要围捕。 But depending on your needs, you might want RoundingMode.CEILING instead; 但是根据您的需要,您可能需要RoundingMode.CEILING ; it depends on what you want -451.2 to become ( -452 [ UP ] or -451 [ CEILING ]). 这取决于你想要什么-451.2-452 [ UP ]或-451 [ CEILING ])。 See RoundingMode for more. 有关更多信息,请参阅RoundingMode

使用:

value.setScale(0, RoundingMode.CEILING);

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; 请注意,由于BigDecimal对象是不可变的,因此调用setScale方法不会导致原始对象被修改,这与通常使用名为setX mutate field X的方法的惯例相反。相反,setScale返回具有适当比例的对象; the returned object may or may not be newly allocated. 返回的对象可能会或可能不会被新分配。

public void test() {
  BigDecimal value = new BigDecimal(450.90);
  System.out.println(value.setScale(0, RoundingMode.HALF_UP)); // prints 451
}

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 BigDecimal is 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; 缩放/舍入操作(setScale和round)返回一个BigDecimal,其值大约(或精确地)等于操作数的值,但其标度或精度是指定值; that is, they increase or decrease the precision of the stored number with minimal effect on its value. 也就是说,它们会增加或减少存储数字的精度,而对其值的影响最小。

use the Math.ceil() function. 使用Math.ceil()函数。 The method ceil gives the smallest integer that is greater than or equal to the argument. 方法ceil给出大于或等于参数的最小整数。

You can read up on it here 你可以在这里阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM