BigDecimal 与 int 的区别?

发布于 2024-12-18 07:04:01 字数 182 浏览 3 评论 0原文

我有两个号码,1.43501.4300。当我减去它们时,我希望得到 50,而不是返回 0.0050。它还需要与 90.2590.10 配合使用,返回 15

I have two numbers, 1.4350 and 1.4300. When I subtract them, instead of returning 0.0050, I'm looking to get 50. It also needs to work with 90.25 and 90.10, returning 15.

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

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

发布评论

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

评论(1

云淡风轻 2024-12-25 07:04:01

您可以使用 BigDecimal.unscaledValue()。这将返回您想要的 BigInteger。

// Two example numbers:
BigDecimal val0 = new BigDecimal("1.4350");
BigDecimal val1 = new BigDecimal("1.4300");


// This might be a method:

if (val0.scale() != val1.scale())
    throws new IllegalArgumentException("Scales are not the same!");

BigDecimal subtr = val0.subtract(val1);
System.out.println(subtr); // Prints 0.0050

BigInteger unscaled = subtr.unscaledValue();
System.out.println(unscaled); // Prints 50

You can use BigDecimal.unscaledValue(). This will return what you want as a BigInteger.

// Two example numbers:
BigDecimal val0 = new BigDecimal("1.4350");
BigDecimal val1 = new BigDecimal("1.4300");


// This might be a method:

if (val0.scale() != val1.scale())
    throws new IllegalArgumentException("Scales are not the same!");

BigDecimal subtr = val0.subtract(val1);
System.out.println(subtr); // Prints 0.0050

BigInteger unscaled = subtr.unscaledValue();
System.out.println(unscaled); // Prints 50
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文