BigDecimal 截取小数点

xiaoxiao2021-02-28  42

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; }

转载请注明原文地址: https://www.6miu.com/read-77688.html

最新回复(0)