Java默认bigdecimal初始值_BigDecimal初始化不要用double类型

在Java中,使用BigDecimal初始化时直接传入double可能导致精度问题。BigDecimal(double val)构造函数不能确保精确转换,例如0.06会变成0.059999999999999997779553950749686919152736663818359375。推荐使用BigDecimal(String str)构造函数确保精确性,如`new BigDecimal("0.06")`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在进行单价、总价相关的计算时,就会用到BigDecimal。

在初始化时,一个不小心,就可能给自己挖坑。

示例如下:

public class BigDecimalInitTest {

public static void main(String[] args) {

BigDecimal amount1=new BigDecimal("0.06");

BigDecimal amount2=new BigDecimal(0.06);

System.out.println(amount1);

System.out.println(amount2);

}

}

运行之后,结果为:

0.06

0.059999999999999997779553950749686919152736663818359375

源码注释

打开BigDecimal的构造方法,可以发现:

/**

* Translates a {@code double} into a {@code BigDecimal} which

* is the exact decimal representation of the {@code double}'s

* binary floating-point value. The scale of the returned

* {@code BigDecimal} is the smallest value such that

* (10scale × val) is an integer.

*

* Notes:

*

*

* The results of this constructor can be somewhat unpredictable.

* One might assume that writing {@code new BigDecimal(0.1)} in

* Java creates a {@code BigDecimal} which is exactly equal to

* 0.1 (an unscaled value of 1, with a scale of 1), but it is

* actually equal to

* 0.1000000000000000055511151231257827021181583404541015625.

* This is because 0.1 cannot be represented exactly as a

* {@code double} (or, for that matter, as a binary fraction of

* any finite length). Thus, the value that is being passed

* in to the constructor is not exactly equal to 0.1,

* appearances notwithstanding.

*

*

* The {@code String} constructor, on the other hand, is

* perfectly predictable: writing {@code new BigDecimal("0.1")}

* creates a {@code BigDecimal} which is exactly equal to

* 0.1, as one would expect. Therefore, it is generally

* recommended that the {@linkplain #BigDecimal(String)

* String constructor} be used in preference to this one.

*

*

* When a {@code double} must be used as a source for a

* {@code BigDecimal}, note that this constructor provides an

* exact conversion; it does not give the same result as

* converting the {@code double} to a {@code String} using the

* {@link Double#toString(double)} method and then using the

* {@link #BigDecimal(String)} constructor. To get that result,

* use the {@code static} {@link #valueOf(double)} method.

*

*

* @param val {@code double} value to be converted to

* {@code BigDecimal}.

* @throws NumberFormatException if {@code val} is infinite or NaN.

*/

public BigDecimal(double val) {

this(val,MathContext.UNLIMITED);

}

大体意思就是,BigDecimal(double val)这个构造方法有时是无法精确预料的,

传入0.1,有可能变成0.1000000000000000055511151231257827021181583404541015625。

因为double类型无法精确地存储0.1

IDEA编码提示

IDEA也会在编码时给出提示。

c64abcf77127c7e92e08cf25f644acfb.png

结论

通过double类型初始化的BigDecimal类型,是不精确的。

最好用String类型的数值字符串来初始化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值