您好,登錄后才能下訂單哦!
這篇文章主要講解了“將BigDecimal轉成字符串出現的坑怎么解決”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“將BigDecimal轉成字符串出現的坑怎么解決”吧!
在開發工程中,在金額方面都會定義bigdecimal類型,當然有時候也需要將金額轉成字符串。我們可能會很自然的寫成 金額.toString()方法如:
costBudgetEntity.getInitTotalAmount().toString()//獲取初始預算金額的字符串
當然當金額過小時,轉成字符串,是沒有任何問題的,但當金額數值較大時,轉成的字符串時科學計數法格式,這往往不是我們想要的格式。
因此
costBudgetEntity.getInitTotalAmount().toString()//金額為12000000輸出的結果為1.2E+7這種的字符串
然后根據這種字符串,無法做一些想要的業務處理
查看BigDecimal的API后,得知有個toPlainString()方法, 此方法的返回類型為String ,它返回此BigDecimal對象的字符串表示形式,不需要任何指數。
/** * 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>'\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字符串
阿里OTS存儲BigDecimal
當BigDecimal數據大于9,999,999時
后就變成科學計數法了。
如10,000,000 就變為1.0E7
接收端應該注意
也需要用BigDecimal,要是使用Integer接收,就可能出現異常
感謝各位的閱讀,以上就是“將BigDecimal轉成字符串出現的坑怎么解決”的內容了,經過本文的學習后,相信大家對將BigDecimal轉成字符串出現的坑怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。