您好,登录后才能下订单哦!
这篇文章主要讲解了“将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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。