浮点数字计算
import java.math.BigDecimal;
public class DoubleUtil {
public static double sum(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.add(bd2).doubleValue();
}
public static double sub(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.subtract(bd2).doubleValue();
}
public static double mul(double d1, double d2) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.multiply(bd2).doubleValue();
}
public static double div(double d1, double d2, int scale) {
BigDecimal bd1 = new BigDecimal(Double.toString(d1));
BigDecimal bd2 = new BigDecimal(Double.toString(d2));
return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
public static String getString(double i) {
String s = String.valueOf(i);
if (s.indexOf(".") > 0) {
s = s.replaceAll("0+?$", "");
s = s.replaceAll("[.]$", "");
}
return s;
}
public static void main(String[] args) {
String i = numberToBits("10000000.01");
System.out.println(i);
}
public static String numberToBits(String number) {
String begin = "";
String end = "";
String[] num = number.split("\\.");
if (num.length > 1) {
begin = num[0];
end = num[1];
} else {
begin = number;
}
return begin.replaceAll("(?<=\\d)(?=(?:\\d{3})+$)", ",") + "." + end;
}
}