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 ...
随机推荐
- 02-oracle中的基础sql
1.SQL SQL(Structured Query Language) 语言是目前主流的关系型数据库上执行数据操作.数据检索以及数据库维护所需要的标准语言,是用户与数据库之间进行交流的接口,许多关系 ...
- JS基础-第1天
JavaScript 第一天笔记 学习目标 了解Javascript的作用及其组成 掌握变量的使用,知道变量的作用是存储数据 掌握变量的命名规范 掌握 JavaScript 的 5 种简单数据类型 掌 ...
- DirectX11--实现一个3D魔方(2)
前言 上一章我们主要讲述了魔方的构造和初始化.纹理的准备工作.目前我还没有打算讲Direct3D 11关于底层绘图的实现,因此接下来这一章的重点是魔方的旋转.因为我们要的是能玩的魔方游戏,而不是一个观 ...
- 【Unity游戏开发】tolua之wrap文件的原理与使用
本文内容转载自:https://www.cnblogs.com/blueberryzzz/p/9672342.html .非常感谢原作者慷慨地授权转载,比心!@blueberryzzz 是位大神,欢迎 ...
- python 去除html 超链接href 如何实现?
今天持久男 在抓取数据的时候发现很多内容都加了锚文本, 这怎么办呢? 没办法只能通过工具解决 我是这样解决的: 例如: soup = BeautifulSoup('<p>Hello < ...
- 剑指Offer-翻转单词顺序列
题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,"st ...
- redis实战笔记
3.1 字符串命令 3.2 列表命令 3.3 集合命令 3.4散列命令 3.5有序集合命令 3.6发布与订阅命令 3.7其他命令 3.7.1 排序 3.7.2键的过期时间
- Python Cookbook 数据结构和算法
1.查找最大或最小的N个元素 import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, n ...
- JAVA进阶21
1.Vector向量 如何选用ArrayList.LinkedList.Vector? ①需要线程安全时,用Vector ②不存在线程安全问题时,并且查找较多用ArrayList(一般使用它) ③不存 ...
- 【codechef】FN/Fibonacci Number
题意 给出 c 和 P ,求最小的非负整数 n 使得 \(Fib(n)=c(mod~ P)\) 其中 P 是质数且 模 10 等于一个完全平方数(也就是说 P 的末位是个完全平方数,那么只能是 1 或 ...