`
中华好儿孙
  • 浏览: 66255 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

用Java原生类BigDecimal计算的问题

    博客分类:
  • java
阅读更多
package test;

import java.math.BigDecimal;

public class TestBigDecimal {
	public static void main(String[] args) {
		BigDecimal n1 = new BigDecimal(2.2);
		BigDecimal n2 = new BigDecimal(2);
		System.out.println("new BigDecimal(2.2) 除以 new BigDecimal(2) 等于 "+n1.divide(n2));
		BigDecimal n3 = new BigDecimal("2.2");
		BigDecimal n4 = new BigDecimal("2");
		System.out.println("new BigDecimal(\"2.2\") 除以 new BigDecimal(\"2\") 等于 "+n3.divide(n4));
	}
}
/*输出:
new BigDecimal(2.2) 除以 new BigDecimal(2) 等于 1.100000000000000088817841970012523233890533447265625
new BigDecimal("2.2") 除以 new BigDecimal("2") 等于 1.1
*/


得出结论:只有将传给BigDecimal构造函数的参数为字符串时, 计算才精确。
分享到:
评论
2 楼 中华好儿孙 2015-08-24  
QuarterLifeForJava 写道
恩,double要这么写:
BigDecimal n1 = new BigDecimal(Double.toString(2.2));  
BigDecimal n2 = new BigDecimal(Double.toString(2));

其实看API它说的很清楚了:
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.

临时要用的时候, 一般不大会细究API, 当发现问题是时候就会去百度, 百度不到才可能去看API, 不管怎样, 用之前先看API确实是个好习惯, 谢了!
1 楼 QuarterLifeForJava 2015-08-24  
恩,double要这么写:
BigDecimal n1 = new BigDecimal(Double.toString(2.2));  
BigDecimal n2 = new BigDecimal(Double.toString(2));

其实看API它说的很清楚了:
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.

相关推荐

Global site tag (gtag.js) - Google Analytics