首页 > 其他 > 详细

2个Double字符串进行

时间:2019-12-16 12:35:46      阅读:81      评论:0      收藏:0      [点我收藏+]
public static int compare(double d1, double d2) {
        if (d1 < d2)
            return -1;           // Neither val is NaN, thisVal is smaller
        if (d1 > d2)
            return 1;            // Neither val is NaN, thisVal is larger

        // Cannot use doubleToRawLongBits because of possibility of NaNs.
        long thisBits    = Double.doubleToLongBits(d1);
        long anotherBits = Double.doubleToLongBits(d2);

        return (thisBits == anotherBits ?  0 : // Values are equal
                (thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                 1));                          // (0.0, -0.0) or (NaN, !NaN)
    }

  Double底层实现,比较的时候不能直接用==逻辑运算符来进行判断,因为会存在精度问题,所以转为long类型进行判断更为合适

2个Double字符串进行

原文:https://www.cnblogs.com/panchangde/p/12048173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!