首页 聚焦 财经 产业 证券 股市 商业 民生 IPO 点评 IPhone客户端 安卓客户端
首页/市场分析

3

bigdecimal比较大小现实相等的两个数字为何输出false
2021-07-12

两个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 大于。

声明:登载此文出于传递信息之目的,本站仅供信息保存,如有异议请联系我们修订!
点赞
编辑:中证
中证新闻网
中证新闻网
中证点评更多

    近日,上市近半年的极兔速递(015...[详细]

    新一轮产业革命和科技变革为我国...[详细]

民生经济更多
中证新闻网
科创板块更多
热搜汇聚更多
  • 合作: 商洽与内容纠错联系方式
  • Email:894204689@qq.com
  • CopyRight@2008-2024 中国证券新闻 All Right Reserved

    工信备案号:备案号京ICP(备)15095275

    中国证券新闻版权所有违者必究