首页 要闻 中国 经济 财经 品牌 点评 会展 综合 | 设为首页
中国品牌要闻网-传递资讯的价值打造品牌的影响
您现在的位置:首页/麻仟点评/ 正文
bigdecimal比较大小现实相等的两个数字为何输出false
来源:
编辑:
时间:2022-07-23

      两个BigDecimal对象大小,相等的判断:一般的对象用equals,但是BigDecimal比较特殊,举个例子:
     BigDecimal a=BigDecimal.valueOf(1.0);
     BigDecimal b=BigDecimal.valueOf(1.000);
     在现实中这两个数字是相等的,但是问题来来了。a.equals(b)结果是false;怎么不相等了呢?


      解决方法:
        if(a.compareTo(b)==0)结果是true
     public int compareTo(BigDecimal val)
          Compares this BigDecimal with the specified BigDecimal.
         Two BigDecimal objects that are equal in value but have a different
         scale (like 2.0 and 2.00) are considered equal by this method.
         This method is provided in preference to individual methods for each of
         the six boolean comparison operators (<, ==, >, >=, !=, <=).
         The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
    Specified by:
    compareTo in interface Comparable<BigDecimal>
    Parameters:
    val - BigDecimal to which this BigDecimal is to be compared.
    Returns:
       -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
    简单翻译:
           结果是  -1 小于 、 0 等于 、 1 大于。

免责声明:本文仅代表作者个人观点,与本网无关。其原创性以及文中陈述文字和内容未经本站证实, 对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺, 请读者仅作参考,并请自行核实相关内容。当事人(单位)如有异议,请参阅《删帖说明》办理。
中国品牌要闻网-传递资讯的价值打造品牌的影响
  • CopyRight@ 2005-2022 中国品牌要闻网
  • 工商营业注册:110101014752254
  • 增值电信业务经营许可证:京A2-2009413