java 关于BigDecimal 和BigInteger
BigDecimal 是比doubule更加精确的数,再算小数的时候由于二进制的原因double 和BigDecimal 假如结果是0.2,那么只能精确到0.199999999,解决这个方案
double 转String
class DoubleToString {
public static String test2(double dou) {
Double dou_obj = new Double(dou);
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
String dou_str = nf.format(dou_obj);
return dou_str;
}
double c = 10.1;
double b = 9.3;
BigDecimal d = new BigDecimal(DoubleToString.test2(c));
BigDecimal e = new BigDecimal(DoubleToString.test2(b));
System.out.println(d.subtract(e).toString());
或者
double c = 10.1;
double b = 9.3;
BigDecimal d = BigDecimal.valueOf(c);
BigDecimal e = BigDecimal.valueOf(b);
System.out.println(d.subtract(e));
BIgInteger是Integer 范围更大的integer 类