首頁 要聞 中國 經(jīng)濟(jì) 財(cái)經(jīng) 品牌 點(diǎn)評 會展 綜合 | 設(shè)為首頁
中國品牌要聞網(wǎng)-傳遞資訊的價(jià)值打造品牌的影響
您現(xiàn)在的位置:首頁/燈飾燈具/ 正文
一般的對象使用equals bigdecimal比較大小較為特殊
來源:
編輯:
時(shí)間:2022-07-22

     兩個BigDecimal對象大小,相等的判斷:一般的對象用equals,但是BigDecimal比較特殊,舉個例子:
     BigDecimal a=BigDecimal.valueOf(1.0);
     BigDecimal b=BigDecimal.valueOf(1.000);
     在現(xiàn)實(shí)中這兩個數(shù)字是相等的,但是問題來來了。a.equals(b)結(jié)果是false;怎么不相等了呢?


    解決方法:
   if(a.compareTo(b)==0)結(jié)果是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.
    簡單翻譯:
           結(jié)果是  -1 小于 、 0 等于 、 1 大于。

免責(zé)聲明:本文僅代表作者個人觀點(diǎn),與本網(wǎng)無關(guān)。其原創(chuàng)性以及文中陳述文字和內(nèi)容未經(jīng)本站證實(shí), 對本文以及其中全部或者部分內(nèi)容、文字的真實(shí)性、完整性、及時(shí)性本站不作任何保證或承諾, 請讀者僅作參考,并請自行核實(shí)相關(guān)內(nèi)容。當(dāng)事人(單位)如有異議,請參閱《刪帖說明》辦理。
中國品牌要聞網(wǎng)-傳遞資訊的價(jià)值打造品牌的影響
  • CopyRight@ 2005-2022 中國品牌要聞網(wǎng)
  • 工商注冊號 430122000189097
  • ICP備案許證:渝ICP備2022012785號