如何在不舍入的情况下截断Java中的小数?

如何在不舍入的情况下截断Java中的小数?,第1张

如何在不舍入的情况下截断Java中的小数

如果您想让事情变得简单快捷。;)

public static void main(String... args) {    double[] values = {0.43678436287643872, 0.4323424556455654, 0.6575643254344554, -0.43678436287643872, -0.4323424556455654, -0.6575643254344554, -0.6575699999999999 };    for (double v : values)         System.out.println(v + " => "+roundDown5(v));}public static double roundDown5(double d) {    return ((long)(d * 1e5)) / 1e5;    //Long typecast will remove the decimals}// Or this. Slightly slower, but faster than creating objects. ;)public static double roundDown5(double d) {    return Math.floor(d * 1e5) / 1e5;}

版画

0.43678436287643874 => 0.436780.4323424556455654 => 0.432340.6575643254344554 => 0.65756-0.43678436287643874 => -0.43678-0.4323424556455654 => -0.43234-0.6575643254344554 => -0.65756-0.6575699999999999 => -0.65756


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5498505.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存