如何将数字的小数组成部分,因此它不影响整个数字的一​​部分?

发布于 2025-01-26 20:56:51 字数 186 浏览 4 评论 0原文

我有这个bigdecimal数字 - 68.65121847597993,我想像这样将其舍入 - 68.7,因此只有小数点的小数零件才能全面舍入,但不是整数。

我尝试使用coundingmode.half_up,但它也将整个数字填写。 有没有办法仅使用Java组合数字的小数部分?

I have this BigDecimal number - 68.65121847597993 and I want to round it like this - 68.7, so only decimical part of number would be rounded up, but not the whole number.

I tried to use RoundingMode.HALF_UP but it rounds up the whole number as well.
Is there a way to round up only decimical part of number using java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

只为一人 2025-02-02 20:56:51

为了表示有很多小数点的数字,我认为您正在使用bigdecimal而不是double,它只能准确地表示大约

bigdecimal 用字符串,双重或其他任何东西初始化,您可以调用 bigdecimal :: setScale 获得所需的值。

例如:

import java.math.*;

public class HelloWorld {
  public static void main(String []args){
    BigDecimal b = new BigDecimal("68.65121847597993");
    BigDecimal rounded = b.setScale(2, RoundingMode.HALF_UP);
    System.out.println(rounded);  // 68.65
  }
}

In order to represent a number with that many decimal points, I presume you are using BigDecimal rather than double, which can only accurately represent approximately 15 significant digits.

Starting from a BigDecimal, which you can initialize with a string, a double, or anything else you have, you can call BigDecimal::setScale to get the value you want.

For example:

import java.math.*;

public class HelloWorld {
  public static void main(String []args){
    BigDecimal b = new BigDecimal("68.65121847597993");
    BigDecimal rounded = b.setScale(2, RoundingMode.HALF_UP);
    System.out.println(rounded);  // 68.65
  }
}
抽个烟儿 2025-02-02 20:56:51

我几乎在其他所有语言中都这样做:

double input = -68.65121847597993;
double rounded = Math.floor(input * 10.0 + 0.5)/10.0;

使用bigdecimal,您有更多选项,例如圆形模式和setScale,如果需要的话。

还有第三方库,例如 decimal4j apache commons数学 - 尽管我没有测试它们。

I would do it as in nearly every other language:

double input = -68.65121847597993;
double rounded = Math.floor(input * 10.0 + 0.5)/10.0;

With a BigDecimal you have more options like a rounding mode and setScale if you need that.

And there are third party libraries like decimal4j or Apache Commons Math - though I didn't test them.

澜川若宁 2025-02-02 20:56:51

您应该尝试

 DecimalFormat df = new DecimalFormat("###.###");

以下是一篇关于它的贝尔登文章:
https://wwwww.baeldung.com/java-round-decimal-decimal-number

希望它有帮助!

You should try this

 DecimalFormat df = new DecimalFormat("###.###");

Here's a Baeldung article about it:
https://www.baeldung.com/java-round-decimal-number

Hope it helps!

四叶草在未来唯美盛开 2025-02-02 20:56:51

您不能真正这样做,因为浮动和双打无法正常工作。

但是,这可以解决您的问题:

round((-68.65121847597993)*10)/10f;

另请参阅: Java Float 123.129456到123.12,没有四舍五入

You cannot really do that because floats and doubles do not work like that.

However, this could solve your problem:

round((-68.65121847597993)*10)/10f;

also see: Java float 123.129456 to 123.12 without rounding

暮色兮凉城 2025-02-02 20:56:51

如果您的价值是双重的,则此方法在编译时需要最低的时间:

Math.round(num * 10d) / 10d

0 =小数的数量

If your value is a double, this method takes the lowest time on compiling:

Math.round(num * 10d) / 10d

Number of 0's = number of decimals

毅然前行 2025-02-02 20:56:51

尝试一下

BigDecimal bd = new BigDecimal(Double.toString(number));
bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP);
return bd.doubleValue();

Try this

BigDecimal bd = new BigDecimal(Double.toString(number));
bd = bd.setScale(decimalPlaces, RoundingMode.HALF_UP);
return bd.doubleValue();
猫性小仙女 2025-02-02 20:56:51

与其更改数字(因此仍可以在后续计算中使用其当前精度),您可以按照以下方式控制显示。

double v = 68.65121847597993;
System.out.printf("%.1f%n", v);

印刷

68.7

Rather than change the number (so its current precision can be still used in subsequent calculations), you can control the display as follows.

double v = 68.65121847597993;
System.out.printf("%.1f%n", v);

prints

68.7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文