将BigDecimal转成字符串为科学计数法的踩坑记录

2023-01-21 0 2,809

目录

BigDecimal转字符串科学计数法踩坑

场景

在开发工程中,在金额方面都会定义bigdecimal类型,当然有时候也需要将金额转成字符串。我们可能会很自然的写成 金额.toString()方法如:

costBudgetEntity.getInitTotalAmount().toString()//获取初始预算金额的字符串

当然当金额过小时,转成字符串,是没有任何问题的,但当金额数值较大时,转成的字符串时科学计数法格式,这往往不是我们想要的格式。

将BigDecimal转成字符串为科学计数法的踩坑记录

因此

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查看:

将BigDecimal转成字符串为科学计数法的踩坑记录

costBudgetEntity.getInitTotalAmount().toPlainString() //金额为12000000输出的结果为12000000字符串

案例演示

将BigDecimal转成字符串为科学计数法的踩坑记录

BigDecimal变科学计数法

阿里OTS存储BigDecimal

当BigDecimal数据大于9,999,999时

后就变成科学计数法了。

如10,000,000 就变为1.0E7

接收端应该注意

也需要用BigDecimal,要是使用Integer接收,就可能出现异常

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 将BigDecimal转成字符串为科学计数法的踩坑记录 http://www.20zxx.cn/463131/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务