欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

場景

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

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

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

因此

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

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

解決

查看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輸出的結(jié)果為12000000字符串

案例演示

BigDecimal變科學(xué)計(jì)數(shù)法

阿里OTS存儲BigDecimal

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

后就變成科學(xué)計(jì)數(shù)法了。

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

接收端應(yīng)該注意

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

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

相關(guān)文章

  • springboot如何通過@Value,@ConfigurationProperties獲取配置

    springboot如何通過@Value,@ConfigurationProperties獲取配置

    這篇文章主要介紹了springboot如何通過@Value,@ConfigurationProperties獲取配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 自定義mybatis插件如何實(shí)現(xiàn)sql日志打印

    自定義mybatis插件如何實(shí)現(xiàn)sql日志打印

    這篇文章主要介紹了自定義mybatis插件如何實(shí)現(xiàn)sql日志打印問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • ssm項(xiàng)目改造spring?boot項(xiàng)目完整步驟

    ssm項(xiàng)目改造spring?boot項(xiàng)目完整步驟

    Spring?Boot現(xiàn)在已經(jīng)成為Java開發(fā)領(lǐng)域的一顆璀璨明珠,它本身是包容萬象的,可以跟各種技術(shù)集成,下面這篇文章主要給大家介紹了關(guān)于ssm項(xiàng)目改造spring?boot項(xiàng)目的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • spring 如何將配置信息注入靜態(tài)變量的方法

    spring 如何將配置信息注入靜態(tài)變量的方法

    本篇文章主要介紹了spring 如何將配置信息注入靜態(tài)變量的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • java項(xiàng)目中的多線程實(shí)踐記錄

    java項(xiàng)目中的多線程實(shí)踐記錄

    項(xiàng)目開發(fā)中對于一些數(shù)據(jù)的處理需要用到多線程,比如文件的批量上傳,數(shù)據(jù)庫的分批寫入,大文件的分段下載等,主要涉及到多線程的一些知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下
    2021-11-11
  • 深入理解Java三大特性中的多態(tài)

    深入理解Java三大特性中的多態(tài)

    多態(tài)性是對象多種表現(xiàn)形式的體現(xiàn)。在面向?qū)ο笾校畛R姷亩鄳B(tài)發(fā)生在使用父類的引用來引用子類的對象。下面這篇文章主要給大家深入的介紹了Java三大特性中多態(tài)的相關(guān)資料,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • 簡單總結(jié)單例模式的4種寫法

    簡單總結(jié)單例模式的4種寫法

    今天帶大家學(xué)習(xí)java的相關(guān)知識,文章圍繞著單例模式的4種寫法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 復(fù)雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實(shí)現(xiàn)

    復(fù)雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實(shí)現(xiàn)

    這篇文章主要介紹了復(fù)雜JSON字符串轉(zhuǎn)換為Java嵌套對象的實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot如何對LocalDateTime進(jìn)行格式化并解析

    SpringBoot如何對LocalDateTime進(jìn)行格式化并解析

    這篇文章主要介紹了SpringBoot如何對LocalDateTime進(jìn)行格式化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java在PDF中添加表格過程詳解

    Java在PDF中添加表格過程詳解

    這篇文章主要介紹了Java在PDF中添加表格過程詳解,本文將介紹通過Java編程在PDF文檔中添加表格的方法。添加表格時,可設(shè)置表格邊框、單元格對齊方式、單元格背景色、單元格合并、插入圖片、設(shè)置行高、列寬、字體、字號等,需要的朋友可以參考下
    2019-07-07

最新評論