如何获取小数点后的数字? (java)

发布于 2024-11-10 14:33:26 字数 138 浏览 3 评论 0原文

 double d = 4.321562;

有没有一种简单的方法可以从 d 中单独提取 0.321562 ?我尝试查看数学课,但没有成功。如果这可以在不转换为字符串或转换为其他任何内容的情况下完成,那就更好了。

 double d = 4.321562;

Is there an easy way to extract the 0.321562 on it's own from d? I tried looking in the math class but no luck. If this can be done without converting to string or casting to anything else, even better.

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

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

发布评论

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

评论(3

白日梦 2024-11-17 14:33:26

那么,您可以使用:

double x = d - Math.floor(d);

请注意,由于二进制浮点的工作方式,这不会给您准确 0.321562,因为原始值不准确 4.321562 。如果您确实对精确数字感兴趣,则应该使用 BigDecimal。

Well, you can use:

double x = d - Math.floor(d);

Note that due to the way that binary floating point works, that won't give you exactly 0.321562, as the original value isn't exactly 4.321562. If you're really interested in exact digits, you should use BigDecimal instead.

街角卖回忆 2024-11-17 14:33:26

另一种不使用 Math 来获取分数的方法是转换为 long。

double x = d - (long) d;

当您打印 double 时,toString 将执行少量舍入,因此您不会看到任何舍入错误。但是,当删除整数部分时,舍入就不再足够,并且舍入误差变得明显。

解决这个问题的方法是自己进行舍入或使用 BigDecimal,它允许您控制舍入。

double d = 4.321562;
System.out.println("Double value from toString " + d);
System.out.println("Exact representation " + new BigDecimal(d));
double x = d - (long) d;
System.out.println("Fraction from toString " + x);
System.out.println("Exact value of fraction " + new BigDecimal(x));
System.out.printf("Rounded to 6 places %.6f%n", x);
double x2 = Math.round(x * 1e9) / 1e9;
System.out.println("After rounding to 9 places toString " + x2);
System.out.println("After rounding to 9 places, exact value " + new BigDecimal(x2));

注意

Double value from toString 4.321562
Exact representation 4.321562000000000125510268844664096832275390625
Fraction from toString 0.3215620000000001
Exact value of fraction 0.321562000000000125510268844664096832275390625
Rounded to 6 places 0.321562
After rounding to 9 places toString 0.321562
After rounding to 9 places, exact value 0.32156200000000001448796638214844278991222381591796875

double 的精度有限,如果不使用适当的舍入,您会发现表示问题逐渐出现。这种情况可能发生在您使用 double esp 数字进行的任何计算中,这些数字不是 2 的幂的精确总和。

Another way to get the fraction without using Math is to cast to a long.

double x = d - (long) d;

When you print a double the toString will perform a small amount of rounding so you don't see any rounding error. However, when you remove the integer part, the rounding is no longer enough and the rounding error becomes obvious.

The way around this is to do the rounding yourself or use BigDecimal which allows you to control the rounding.

double d = 4.321562;
System.out.println("Double value from toString " + d);
System.out.println("Exact representation " + new BigDecimal(d));
double x = d - (long) d;
System.out.println("Fraction from toString " + x);
System.out.println("Exact value of fraction " + new BigDecimal(x));
System.out.printf("Rounded to 6 places %.6f%n", x);
double x2 = Math.round(x * 1e9) / 1e9;
System.out.println("After rounding to 9 places toString " + x2);
System.out.println("After rounding to 9 places, exact value " + new BigDecimal(x2));

prints

Double value from toString 4.321562
Exact representation 4.321562000000000125510268844664096832275390625
Fraction from toString 0.3215620000000001
Exact value of fraction 0.321562000000000125510268844664096832275390625
Rounded to 6 places 0.321562
After rounding to 9 places toString 0.321562
After rounding to 9 places, exact value 0.32156200000000001448796638214844278991222381591796875

NOTE: double has limited precision and you can see representation issue creep in if you don't use appropriate rounding. This can happen in any calculation you use with double esp numbers which are not an exact sum of powers of 2.

可是我不能没有你 2024-11-17 14:33:26

使用模:

double d = 3.123 % 1;
assertEquals(0.123, d,0.000001);

Use modulo:

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