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.
*****************************************************************************************
********************* 联系方式:qq:464675856; email: huyangleng3684@sina.com **********************
********** 微信个人账号:mr_daoqi ************ ************* 微信公众号:倒骑的驴 **************
*****************************************************************************************
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了