首页 头条 财经 产业 金融 电商 跨境 物流 会展 English
首页 / 论坛展会 / 正文

一般的对象使用equals bigdecimal比较大小较为特殊

来源:
编辑:电经淘讯
时间:2025-01-27

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

电经快报更多
经济视界更多

方升专访钱塘产业集团韩丽:招商遇冷,

如今提及人工智能,杭州常被首先想起,这得益于“六小龙”爆火—...[详细]

2025能源产业生态论坛召开,京东政企业

6月28日,以“面向‘十五五’,加速构建新型能源体系”为主题的2...[详细]

热点财经更多
  • CopyRight @ 2013-2025 Reserved CN电商经济网版权所有所涉内容免责声明查阅