java使用何种类型表示精确的小数?
问题
java使用何种类型表示精确的小数?
结论
- float和double类型的主要设计目标是为了科学计算和工程计算,速度快,存在精度丢失
- BigDecimal用来表示任意精确浮点数运算的类,在商业应用环境下,不存在精度丢失
cookie
- 接受double实例化BigDecimal存在精度丢失问题,如果一定需要使用double,则先Double.toString()转成String后再行实例化;
- 建议接受String去实例化BigDecimal
NOTE
BigDecimal --> Long 存在精度丢失
public long longValue()
Converts this
BigDecimal
to a long
. This conversion is analogous to the narrowing primitive conversion from double
to short
as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this BigDecimal
will be discarded, and if the resulting "BigInteger
" is too big to fit in a long
, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this BigDecimal
value as well as return a result with the opposite sign.new BigDecimal(double d)存在精度丢失
public BigDecimal(double val)
Translates a
double
into a BigDecimal
which is the exact decimal representation of the double
's binary floating-point value. The scale of the returned 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
new BigDecimal(0.1)
in Java creates aBigDecimal
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 adouble
(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
String
constructor, on the other hand, is perfectly predictable: writingnew BigDecimal("0.1")
creates aBigDecimal
which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one. - When a
double
must be used as a source for aBigDecimal
, note that this constructor provides an exact conversion; it does not give the same result as converting thedouble
to aString
using theDouble.toString(double)
method and then using theBigDecimal(String)
constructor. To get that result, use thestatic
valueOf(double)
method.
- Parameters:
val
-double
value to be converted toBigDecimal
.- Throws:
NumberFormatException
- ifval
is infinite or NaN.