public class TestDemo { public static void main(String[] args) { String format = new DecimalFormat("##0.#######").format(10.000001); System.out.println(format); DecimalFormat f = new DecimalFormat("#,##0.00"); f.setRoundingMode(RoundingMode.HALF_UP); String format1 = f.format(1.126); System.out.println(format1); }} output:1.12
DecimalFormat f = new DecimalFormat("#,##0.00");
f.setRoundingMode(RoundingMode.HALF_UP);
double d = 1.125;
System.out.println(f.format(d)); // 输出 1.12
d = 1.135;
System.out.println(f.format(d)); // 输出 1.14
public static float div(int v1, int v2, int scale) {
if (v2 != 0) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divid(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
} else {
return 0;
}
}
//scale表示保留几位小数
时间: 2024-10-20 00:11:02