最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

將BigDecimal轉(zhuǎn)成字符串為科學計數(shù)法的踩坑記錄

 更新時間:2022年06月18日 16:19:27   作者:滿腦子代碼的祝大朋  
這篇文章主要介紹了將BigDecimal轉(zhuǎn)成字符串為科學計數(shù)法的踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

BigDecimal轉(zhuǎn)字符串為科學計數(shù)法踩坑

場景

在開發(fā)工程中,在金額方面都會定義bigdecimal類型,當然有時候也需要將金額轉(zhuǎn)成字符串。我們可能會很自然的寫成 金額.toString()方法如:

costBudgetEntity.getInitTotalAmount().toString()//獲取初始預算金額的字符串

當然當金額過小時,轉(zhuǎn)成字符串,是沒有任何問題的,但當金額數(shù)值較大時,轉(zhuǎn)成的字符串時科學計數(shù)法格式,這往往不是我們想要的格式。

因此

costBudgetEntity.getInitTotalAmount().toString()//金額為12000000輸出的結果為1.2E+7這種的字符串 

然后根據(jù)這種字符串,無法做一些想要的業(yè)務處理

解決

查看BigDecimal的API后,得知有個toPlainString()方法, 此方法的返回類型為String ,它返回此BigDecimal對象的字符串表示形式,不需要任何指數(shù)。

/**
* Returns a string representation of this {@code BigDecimal}
* without an exponent field.  For values with a positive scale,
* the number of digits to the right of the decimal point is used
* to indicate scale.  For values with a zero or negative scale,
* the resulting string is generated as if the value were
* converted to a numerically equal value with zero scale and as
* if all the trailing zeros of the zero scale value were present
* in the result.
*
* The entire string is prefixed by a minus sign character '-'
* (<tt>'&#92;u002D'</tt>) if the unscaled value is less than
* zero. No sign character is prefixed if the unscaled value is
* zero or positive.
*
* Note that if the result of this method is passed to the
* {@linkplain #BigDecimal(String) string constructor}, only the
* numerical value of this {@code BigDecimal} will necessarily be
* recovered; the representation of the new {@code BigDecimal}
* may have a different scale.  In particular, if this
* {@code BigDecimal} has a negative scale, the string resulting
* from this method will have a scale of zero when processed by
* the string constructor.
*
* (This method behaves analogously to the {@code toString}
* method in 1.4 and earlier releases.)
*
* @return a string representation of this {@code BigDecimal}
* without an exponent field.
* @since 1.5
* @see #toString()
* @see #toEngineeringString()
*/
public String toPlainString() {
    if(scale==0) {
        if(intCompact!=INFLATED) {
            return Long.toString(intCompact);
        } else {
            return intVal.toString();
        }
    }
    if(this.scale<0) { // No decimal point
        if(signum()==0) {
            return "0";
        }
        int tailingZeros = checkScaleNonZero((-(long)scale));
        StringBuilder buf;
        if(intCompact!=INFLATED) {
            buf = new StringBuilder(20+tailingZeros);
            buf.append(intCompact);
        } else {
            String str = intVal.toString();
            buf = new StringBuilder(str.length()+tailingZeros);
            buf.append(str);
        }
        for (int i = 0; i < tailingZeros; i++)
            buf.append('0');
        return buf.toString();
    }
    String str ;
    if(intCompact!=INFLATED) {
        str = Long.toString(Math.abs(intCompact));
    } else {
        str = intVal.abs().toString();
    }
    return getValueString(signum(), str, scale);
}

此時,我們在debug查看:

costBudgetEntity.getInitTotalAmount().toPlainString() //金額為12000000輸出的結果為12000000字符串

案例演示

BigDecimal變科學計數(shù)法

阿里OTS存儲BigDecimal

當BigDecimal數(shù)據(jù)大于9,999,999時

后就變成科學計數(shù)法了。

如10,000,000 就變?yōu)?.0E7

接收端應該注意

也需要用BigDecimal,要是使用Integer接收,就可能出現(xiàn)異常

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • SpringBoot實現(xiàn)Thymeleaf驗證碼生成

    SpringBoot實現(xiàn)Thymeleaf驗證碼生成

    本文使用SpringBoot實現(xiàn)Thymeleaf驗證碼生成,使用后臺返回驗證碼圖片,驗證碼存到session中后端實現(xiàn)校驗,前端只展示驗證碼圖片。感興趣的可以了解下
    2021-05-05
  • java中統(tǒng)一返回前端格式及統(tǒng)一結果處理返回詳解

    java中統(tǒng)一返回前端格式及統(tǒng)一結果處理返回詳解

    這篇文章主要介紹了統(tǒng)一結果處理的重要性,以及如何在SpringBoot項目中定義和使用統(tǒng)一結果返回類,通過構造器私有化和靜態(tài)方法ok、error,提供了成功和失敗的統(tǒng)一響應格式,需要的朋友可以參考下
    2025-02-02
  • 淺談java中異步多線程超時導致的服務異常

    淺談java中異步多線程超時導致的服務異常

    下面小編就為大家?guī)硪黄獪\談java中異步多線程超時導致的服務異常。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • springboot從application.properties中注入list,?map方式

    springboot從application.properties中注入list,?map方式

    這篇文章主要介紹了springboot從application.properties中注入list,map方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • idea 創(chuàng)建properties配置文件的步驟

    idea 創(chuàng)建properties配置文件的步驟

    這篇文章主要介紹了idea 創(chuàng)建properties配置文件的步驟,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • FastJson時間格式化問題避坑經(jīng)驗分享

    FastJson時間格式化問題避坑經(jīng)驗分享

    這篇文章主要為大家介紹了FastJson時間格式化問題避坑經(jīng)驗分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Java?IO流之StringWriter和StringReader用法分析

    Java?IO流之StringWriter和StringReader用法分析

    這篇文章主要介紹了Java?IO流之StringWriter和StringReader用法分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java基礎之初始化ArrayList時直接賦值的4種方式總結

    java基礎之初始化ArrayList時直接賦值的4種方式總結

    ArrayList是Java中的一個類,它是Java集合框架中的一部分,用于實現(xiàn)動態(tài)數(shù)組,下面這篇文章主要給大家介紹了關于java基礎之初始化ArrayList時直接賦值的4種方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-07-07
  • 淺析java class 文件

    淺析java class 文件

    以下是對java中的class文件進行了詳細的介紹,需要的朋友可以過來參考下
    2013-08-08
  • SpringBoot中的響應式web應用詳解

    SpringBoot中的響應式web應用詳解

    這篇文章主要介紹了SpringBoot中的響應式web應用詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11

最新評論

平塘县| 禄劝| 始兴县| 蛟河市| 静安区| 邢台县| 平顶山市| 长宁县| 通化市| 焦作市| 舟曲县| 崇义县| 亚东县| 海兴县| 晋江市| 平武县| 库尔勒市| 屯昌县| 东城区| 新晃| 邹城市| 屯门区| 宜昌市| 泰和县| 子洲县| 孟连| 木里| 平凉市| 冕宁县| 兰考县| 包头市| 白玉县| 邓州市| 靖江市| 忻州市| 三穗县| 吉首市| 密云县| 湘潭县| 冀州市| 光山县|