Java的浮点精确计算

Java在做浮点运算的时候由于float和double容易丢失精度, 所以引入了BigDecimal来做精确的运算,它是不可变的任意精度的10进制数。
/**
 * BigDecimal
 * 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 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.
 *	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.
 *	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.
 *	Parameters:
 *	val - double value to be converted to BigDecimal.
 *  
 *  public BigDecimal(String val) 推荐,准确

 */
package math;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// float和double在计算时有时不准确,要准确计算应该使用BigDecimal类
		// System.out.println(0.01 + 0.09);//0.09999999999999999
		// System.out.println(1 - 0.32);//0.6799999999999999
		// System.out.println(1.015 * 100);//101.49999999999999
		// System.out.println(1.301 / 100);//0.013009999999999999

		// 使用BigDecimal类
		BigDecimal bd1 = new BigDecimal("0.01");
		BigDecimal bd2 = new BigDecimal("0.09");
		System.out.println("add:" + bd1.add(bd2));
		System.out.println("--------------------");

		BigDecimal bd3 = new BigDecimal("1");
		BigDecimal bd4 = new BigDecimal("0.32");
		System.out.println("subtract:" + bd3.subtract(bd4));
		System.out.println("--------------------");

		BigDecimal bd5 = new BigDecimal("1.015");
		BigDecimal bd6 = new BigDecimal("100.0");
		System.out.println("multiply:" + bd5.multiply(bd6));
		System.out.println("--------------------");

		BigDecimal bd7 = new BigDecimal("1301");
		BigDecimal bd8 = new BigDecimal("100.0");
		System.out.println("divide:" + bd7.divide(bd8));
		System.out.println("--------------------");

		BigDecimal bd9 = new BigDecimal("1301.00");
		BigDecimal bd10 = new BigDecimal("100.0");
		System.out.println("divide,round mode ceiling:"
				+ bd9.divide(bd10, 1, RoundingMode.HALF_UP));
		System.out.println("--------------------");
	}

}
 
结果:
add:0.10
--------------------
subtract:0.68
--------------------
multiply:101.5000
--------------------
divide:13.01
--------------------
divide,round mode ceiling:13.0
--------------------

你可能感兴趣的:(BigDecimal,浮点数,精确计算)