BigDecimal.valueOf
Those are two separate questions: "What should I use for BigDecimal
?" and "What do I do in general?"
For BigDecimal
: this is a bit tricky, because they don't do the same thing. BigDecimal.valueOf(double)
will use the canonical String
representation of the double
value passed in to instantiate the BigDecimal
object. In other words: The value of the BigDecimal
object will be what you see when you do System.out.println(d)
.
If you use new BigDecimal(d)
however, then the BigDecimal
will try to represent the double
value as accurately as possible. This will usually result in a lot more digits being stored than you want. Strictly speaking, it's more correct than valueOf()
, but it's a lot less intuitive.
There's a nice explanation of this in the JavaDoc:
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.
In general, if the result is the same (i.e. not in the case of BigDecimal
, but in most other cases), then valueOf()
should be preferred: it can do caching of common values (as seen on Integer.valueOf()
) and it can even change the caching behaviour without the caller having to be changed. new
will always instantiate a new value, even if not necessary (best example: new Boolean(true)
vs. Boolean.valueOf(true)
).
- 3Awesome answer (+1) – Sean Patrick Floyd Aug 25 '11 at 7:45
- This also explains my question: stackoverflow.com/questions/15685705/… – Christian Mar 28 '13 at 17:22
- 1@Joachim, it wasn't clear. Is
new BigDecimal()
better thanBigDecimal.valueOf()
? – ryvantage Dec 24 '13 at 22:20 - 4@ryvantage: if one where strictly better than the other then there would be no need for both and my answer would be much shorter. They don't do the same thing, so you can't rank them at like that. – Joachim Sauer Dec 24 '13 at 22:27
- 2@JoachimSauer, ok sorry I should've been more specific. Would you mind giving an example of when
new BigDecimal()
would be preferred and an example of whenBigDecimal.valueOf()
would be preferred? – ryvantage Dec 24 '13 at 22:43
If you are using your BigDecimal
objects to store currency values, then I strongly recommend that you do NOT involve any double values anywhere in their calculations.
As stated in another answer, there are known accuracy issues with double values and these will come back to haunt you big time.
Once you get past that, the answer to your question is simple. Always use the constructor method with the String value as the argument to the constructor, as there is no valueOf
method for String
.
If you want proof, try the following:
BigDecimal bd1 = new BigDecimal(0.01);
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("bd1 = " + bd1);
System.out.println("bd2 = " + bd2);
You'll get the following output:
bd1 = 0.01000000000000000020816681711721685132943093776702880859375
bd2 = 0.01
BigDecimal.valueOf的更多相关文章
- java 大数据处理类 BigDecimal 解析
这两天,由于我的必修课概率论里经常要用到排列组合的计算,感觉很麻烦,加上现代智能手机的计算器是没有这方面功能的. 所以,就自己动手写了个安卓的 排列组合 计算器,用了一天,发现有很大的问题,阶乘达百亿 ...
- 关于BigDecimal 和 double 类型保存金钱,以及精度问题,银行家舍入法
1. BigDecimal 类型数据 的创建,构造函数 有 public BigDecimal(BigInteger intVal, long val, int scale, int prec); p ...
- 使用BigDecimal进行精确运算以及格式化输出数字
一.引言 借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供 ...
- 使用BigDecimal进行精确运算
首先我们先来看如下代码示例: 1 public class Test_1 { 2 public static void main(String[] args) { 3 System.out.print ...
- Java 中浮点数---------BigDecimal和double(初探)
为什么要使用 bigdecimal? 借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了 ...
- Java BigDecimal详解
借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供 较为精确的快速近 ...
- Java大数处理类:BigInteger类和BigDecimal类
当我们要处理非常大的数据时,平常用的数据类型已不足以表示,在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,这两个类在理论上只要计算机内存足够大就能够表示无线 ...
- BigDecimal 转换类型
使用BigDecimal类来进行计算的时候,主要分为以下步骤: 1.用float或者double变量构建BigDecimal对象. 2.通过调用BigDecimal的加,减,乘,除等相应的方法进行算术 ...
- BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result的解决办法
BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represent ...
随机推荐
- c++对象的存储空间
1. 非静态成员 2. 静态成员变量 静态成员变量不占对象的内存空间 3. 成员函数 成员函数不占内存空间 4. 析构函数 5. 类中有虚析构函数 6. 继承空类和多重继承空类存储空间的计算 7. t ...
- 关于FastDBF库读写ArcGis dbf文件的小bug
该库托管于GitHub,地址:https://github.com/SocialExplorer/FastDBF 贡献者应该都是老外,所以…… 1.解析文件头,字段名部分如果有中文命名字段会出错 在D ...
- Mysql 根据一个表数据更新另外一个表
方法一: update 更新表 set 字段 = (select 参考数据 from 参考表 where 参考表.id = 更新表.id); update table_2 m set m.colum ...
- 小米5如何支持AT&T网络运营商
最近在美帝生活,买了一张H2O的电话卡,但是很不幸,没有办法连接到网络. 在网上翻看了好多帖子,提到说修改APN,但是基本上都没提怎么修改,不知道这些大神都是怎么修改的.于是寻求google帮助,最后 ...
- gunicorn+anaconda+nginx部署django项目(ubuntu)
首先进入conda 虚拟环境: source activate test 安装gunicorn: pip install gunicorn 运行gunicorn gunicorn -w 2 -b 12 ...
- 关于Java的volatile
volatile的作用 1.防止指令重排序 首先要理解什么是指令重排序?指令重排序的利弊?后续举例说明 2.多线程访问共享资源时,缓解synchronized重量级锁带来的性能问题 但是volatil ...
- hyperledger fabric部署总结
之前在有道云笔记上分享过,但想想还是搬到这里来吧,以后统一方便整理自己的知识进入正题.... 之前在调研 hyperledger fabric,其实部署说明官网都有,只是东西都是国外的照着操作也会遇到 ...
- dubbo基本信息
1.Dubbo是什么? Dubbo是阿里巴巴开源的基于 Java 的高性能 RPC 分布式服务框架,现已成为 Apache 基金会孵化项目. 面试官问你如果这个都不清楚,那下面的就没必要问了. 官网: ...
- Win 10中使用图片查看器
在Win10中,照片应用提供了时间线.专辑等更丰富的图片管理功能,但是对于基于文件夹打开浏览图片的方式显得笨拙, 放大缩小操作略繁琐,有时还会出现当前文件夹图片加载迟缓导致无法快速浏览的问题. 此时你 ...
- pwnable.tw silver_bullet
产生漏洞的原因 int __cdecl power_up(char *dest) { char s; // [esp+0h] [ebp-34h] size_t new_len; // [esp+30h ...