BigDecimal 截取小数点

来源:互联网 发布:如何复制知乎的文字 编辑:程序博客网 时间:2024/05/16 01:12

public static BigDecimal cutBigDecimal(BigDecimal math, int bit) {
if (math == null) {
return new BigDecimal(0);
}
BigDecimal bigDecimal = null;
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
nf.setMaximumFractionDigits(100);
nf.setMaximumIntegerDigits(100);
String mathProxy = nf.format(math);
BigDecimal bg = new BigDecimal(mathProxy);
String num;
String mathProxy2 = “”;
if (mathProxy.indexOf(“.”) > 0) {
num = mathProxy.substring(mathProxy.indexOf(“.”) + 1);
mathProxy2 = mathProxy.substring(0, mathProxy.indexOf(“.”) + 1);
if (bit == 4 && num.length() >= 4) {
num = num.substring(0, bit);
mathProxy2 = mathProxy2.concat(num);
} else if (bit == 2 && num.length() >= 2) {
num = num.substring(0, bit);
mathProxy2 = mathProxy2.concat(num);
} else if (bit == 3 && num.length() >= 3) {
num = num.substring(0, bit);
mathProxy2 = mathProxy2.concat(num);
} else if (bit == 8 && num.length() >= 8) {
num = num.substring(0, bit);
mathProxy2 = mathProxy2.concat(num);
} else {
return bg;
}
} else {
mathProxy2 = nf.format(math);
}
bigDecimal = new BigDecimal(mathProxy2);
return bigDecimal;
}

原创粉丝点击