对于高精度或者运算数较大的计算,应该使用BigDecimal类(浮点数)或者BigInteger类(整数)来实现,否则JAVA基本类型的数据无法保证浮点数的精确,如下例。
import java.math.BigDecimal;
import java.util.Scanner;
//计算阶乘和
public class WhileCal {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入t的值:");
int t=sc.nextInt();
BigDecimal sum=new BigDecimal(0.0);
BigDecimal factorial=new BigDecimal(1.0);
int i=1;
while(i<=t){
sum=sum.add(factorial);//求各阶乘之和
++i;
factorial=factorial.multiply(new BigDecimal(1.0/i));//求阶乘
}
System.out.println("1+1/2!......+1/t!="+sum);
}
}
以下内容摘自网络:
BigDecimal介绍
BigDecimal是Java提供的一个不变的、任意精度的有符号十进制数对象。它提供了四个构造器,有两个是用BigInteger构造,在这里我们不关心,我们重点看用double和String构造的两个构造器(有关BigInteger详细介绍请查阅j2se API文档)。
BigDecimal(double val) Translates a double into a BigDecimal. |
BigDecimal(String val) Translates the String representation of a BigDecimal into a BigDecimal. |
BigDecimal(double)是把一个double类型十进制数构造为一个BigDecimal对象实例。
BigDecimal(String)是把一个以String表示的BigDecimal对象构造为BigDecimal对象实例。
习惯上,对于浮点数我们都会定义为double或float,但BigDecimal API文档中对于BigDecimal(double)有这么一段话:
Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .10000000000000000555111512312578 27021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances notwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one
下面对这段话做简单解释:
注意:这个构造器的结果可能会有不可预知的结果。有人可能设想new BigDecimal(.1)等于.1是正确的,但它实际上是等于.1000000000000000055511151231257827021181583404541015625,这就是为什么.1不能用一个double精确表示的原因,因此,这个被放进构造器中的长值并不精确的等于.1,尽管外观看起来是相等的。
然而(String)构造器,则完全可预知的,new BigDecimal(“.1”)如同期望的那样精确的等于.1,因此,(String)构造器是被优先推荐使用的。
看下面的结果:
System.out.println(new BigDecimal(123456789.02).toString());
System.out.println(new BigDecimal("123456789.02").toString());
输出为:
123456789.01999999582767486572265625
123456789.02
现在我们知道,如果需要精确计算,非要用String来够造BigDecimal不可!
实现方案
现在我们已经知道怎么解决这个问题了,原则上是使用BigDecimal(String)构造器,我们建议,在商业应用开发中,涉及金额等浮点数计算的数据,全部定义为String,数据库中可定义为字符型字段,在需要使用这些数据进行运算的时候,使用BigDecimal(String)构造BigDecimal对象进行运算,保证数据的精确计算。同时避免了科学记数法的出现。如果科学记数表示法在应用中不是一种负担的话,可以考虑定义为浮点类型。
这里我们提供了一个工具类,定义浮点数的加、减、乘、除和四舍五入等运算方法。以供参考。
源文件MathExtend.java:
import java.math.BigDecimal;
public class MathExtend
{
//默认除法运算精度
private static final int DEFAULT_DIV_SCALE = 10;
public static double add(double v1, double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doublue();
}
public static String add(String v1, String v2)
{
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.add(b2).toString();
}
public static double subtract(double v1, double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doublue();
}
public static String subtract(String v1, String v2)
{
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.subtract(b2).toString();
}
public static double multiply(double v1, double v2)
{
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doublue();
}
public static String multiply(String v1, String v2)
{
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.multiply(b2).toString();
}
public static double divide(double v1, double v2)
{
return divide(v1, v2, DEFAULT_DIV_SCALE);
}
public static double divide(double v1,double v2, int scale)
{
return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);
}
public static double divide(double v1,double v2,int scale, int round_mode){
if(scale < 0)
{
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2, scale, round_mode).doublue();
}
public static String divide(String v1, String v2)
{
return divide(v1, v2, DEFAULT_DIV_SCALE);
}
public static String divide(String v1, String v2, int scale)
{
return divide(v1, v2, DEFAULT_DIV_SCALE, BigDecimal.ROUND_HALF_EVEN);
}
public static String divide(String v1, String v2, int scale, int round_mode)
{
if(scale < 0)
{
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(v1);
BigDecimal b2 = new BigDecimal(v2);
return b1.divide(b2, scale, round_mode).toString();
}
public static double round(double v,int scale)
{
return round(v, scale, BigDecimal.ROUND_HALF_EVEN);
}
public static double round(double v, int scale, int round_mode)
{
if(scale<0)
{
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
return b.setScale(scale, round_mode).doublue();
}
public static String round(String v, int scale)
{
return round(v, scale, BigDecimal.ROUND_HALF_EVEN);
}
public static String round(String v, int scale, int round_mode)
{
if(scale<0)
{
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(v);
return b.setScale(scale, round_mode).toString();
}
}