public class text2 {
public static void main(String[] args) {
Number b = 12.6;
Number c = 12.4;
Number d = 12.50;
Number f = 12.5;
Number h = 12.51;
BigDecimal e1 = BigDecimal.valueOf(b.doubleValue());
BigDecimal e2 = BigDecimal.valueOf(c.doubleValue());
BigDecimal e3 = BigDecimal.valueOf(d.doubleValue());
BigDecimal e4 = BigDecimal.valueOf(f.doubleValue());
BigDecimal e5 = BigDecimal.valueOf(h.doubleValue());
double f1 = e1.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f2 = e2.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f3 = e3.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f4 = e4.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f5 = e5.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
}
}
结果
13.0
12.0
12.0
12.0
13.0
进程已结束,退出代码为 0
规则
(1)被修约的数字小于5时,该数字舍去;
(2)被修约的数字大于5时,则进位;
(3)被修约的数字等于5时,要看5前面的数字,若是奇数则进位,若是偶数则将5舍掉,即 修约后末尾数字都成为偶数;若5的后面还有不为“0”的任何数,则此时无论5的前面是奇数还是偶数,均应进位。
举例,用上述规则对下列数据保留3位有效数字:
9.8249=9.82, 9.82671=9.83
9.8350=9.84, 9.83501=9.84
9.8250=9.82, 9.82501=9.83
从统计学的角度,“四舍六入五成双”比“四舍五入”要科学,在大量运算时,它使舍入后的结果误差的均值趋于零,而不是像四舍五入那样逢五就入,导致结果偏向大数,使得误差产生积累进而产生系统误差,“四舍六入五成双”使测量结果受到舍入误差的影响降到最低。
例如:1.15+1.25+1.35+1.45=5.2,若按四舍五入取一位小数计算:
1.2+1.3+1.4+1.5=5.4
按“四舍六入五成双”计算,1.2+1.2+1.4+1.4=5.2,舍入后的结果更能反映实际结果。
尤其是在化学领域应用广泛,在计算“分析化学”、“化学平衡”时经常需要使用“四舍六入五成双”这种较精确的修约方法。这样得到的结果较精确,而且运算量相对来说也不大,十分有用。
四舍五入三种常见方式(包含保留小数)
Math.round
// Math.round
public class Test {
public static void main(String[] args) {
System.out.println("小数点后第一位=5");
System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));//12
System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));//-11
System.out.println();
System.out.println("小数点后第一位<5");
System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));//11
System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));//-11
System.out.println();
System.out.println("小数点后第一位>5");
System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));//12
System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));//-12
}
}
BigDecimal
public class text2 {
public static void main(String[] args) {
Number b = 12.6;
Number c = 12.4;
Number d = 12.50;
Number f = 12.5;
Number h = 12.51;
BigDecimal e1 = BigDecimal.valueOf(b.doubleValue());
BigDecimal e2 = BigDecimal.valueOf(c.doubleValue());
BigDecimal e3 = BigDecimal.valueOf(d.doubleValue());
BigDecimal e4 = BigDecimal.valueOf(f.doubleValue());
BigDecimal e5 = BigDecimal.valueOf(h.doubleValue());
double f1 = e1.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f2 = e2.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f3 = e3.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f4 = e4.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
double f5 = e5.setScale(0, RoundingMode.HALF_EVEN).doubleValue();
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
}
}
DecimalFormat
public static void main( String[] args ) {
//数字格式化,#和0都是占位符,#:当要格式化的数字位数不足时#会补空白;0:当要格式化的数字 位数不足时会补0
DecimalFormat df1 = new DecimalFormat("#.##");//数字格式化
System.out.println(df1.format(0.01));
DecimalFormat df2 = new DecimalFormat("0.000");//数字格式化
System.out.println(df2.format(0.01));
//数字以%的形式展示,小数转百分比自动转换
DecimalFormat df3 = new DecimalFormat("#0.000%");//数字格式化
System.out.println(df3.format(0.11));
//数字以%的形式展示,小数转百分比自动转换
DecimalFormat df4 = new DecimalFormat("#.##%");//数字格式化
System.out.println(df4.format(0.01111));
//用","将数字按3位隔开
DecimalFormat df5 = new DecimalFormat(",###");//数字格式化
System.out.println(df5.format(981725025));
DecimalFormat df6 = new DecimalFormat("顶顶顶顶顶,###");//数字格式化
System.out.println(df6.format(981725025));
DecimalFormat df7 = new DecimalFormat("###,###.000");//数字格式化
System.out.println(df7.format(981725025.981725025));//--会四舍五入
}