BigDecimal(double)” should not be used
来源:互联网 发布:声音编辑软件中文版 编辑:程序博客网 时间:2024/05/16 04:25
BigDecimal(double)” should not be used
级别:bug
Because of floating point imprecision, you’re unlikely to get the value you expect from the BigDecimal(double) constructor.
From the JavaDocs:
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.
Instead, you should use BigDecimal.valueOf, which uses a string under the covers to eliminate floating point rounding errors.
Noncompliant Code Example
double d = 1.1;
BigDecimal bd1 = new BigDecimal(d); // Noncompliant; see comment above
BigDecimal bd2 = new BigDecimal(1.1); // Noncompliant; same result
Compliant Solution
double d = 1.1;
BigDecimal bd1 = BigDecimal.valueOf(d);
BigDecimal bd2 = BigDecimal.valueOf(1.1);
To chinese:
不要使用BigDecimal(double)去构造一个BigDecimal对象,因为double类型在计算机表示方法中并不精确,因此,BigDecimal(double)构造出来的对象很可能不是预期的大小,若一定要使用double类型去构造一个BigDecimal对象,请使用BigDecimal.valueOf方法,该方法先将double转换为String,再通过String构造BigDecimal对象,通常更建议使用public BigDecimal(String val)构造方法。
- BigDecimal(double)” should not be used
- Why the "volatile" type class should not be used
- the `gets' function is dangerous and should not be used.
- the update-modules command is deprecated and should not be used
- warning: the `gets' function is dangerous and should not be used.
- warning: the `gets' function is dangerous and should not be used.
- “warning: the `gets' function is dangerous and should not be used.
- The server farm account should not be used for other services
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- Linux下开发warning:"the `gets' function is dangerous and should not be used"
- ubuntu RPM should not be used directly install RPM packages, use Alien instead!
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- warning: the `gets' function is dangerous and should not be used.
- Ubuntu下RPM should not be used directly install RPM packages, use Alien instead!
- ubuntu RPM should not be used directly install RPM packages, use Alien instead!
- RPM should not be used directly install RPM packages, use Alien instead!
- rpm:RPM should not be used directly install RPM packages, use Alien instead!
- 2017.08.09
- js内存泄露的几种情况
- hadoop 分片与分块,map task和reduce task的理解
- Resource interpreted as Stylesheet but transferred with MIME type text/plain
- api接口调用实例(response json_encode json)创建展视互动直播间,
- BigDecimal(double)” should not be used
- RN开发实践——仿携程App(三)
- Java Collections.EMPTY_LIST 和 Collections.emptyList()的区别
- LeetCode LinkedList 系列解集
- 我的前半生:贺涵暴露20条职场残酷真相
- [计算机组成原理]--关于typing(类型指派)
- Map集合中value()方法与keySet()、entrySet()区别
- 重写equals步骤及一些例子
- form表单的基本用法