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.
Specified by:
longValue in class  Number
Returns:
this  BigDecimal converted to a  long.

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:

  1. The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a 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 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.
  2. The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal 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.
  3. When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
Parameters:
val -  double value to be converted to  BigDecimal.
Throws:
NumberFormatException - if  val is infinite or NaN.

 

转载于:https://www.cnblogs.com/daoqidelv/p/10373838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值