java四舍五入保留2位小数
的小坑,附例子分享:java 1.8
RoundingMode.HALF_UPimport java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; public class test02 { public static void main(String[] args) throws Exception { concurrency(); } private static void concurrency() throws Exception { Double d1=82.365; Double d2=82.3651; Double d3=82.265; Double d4=82.375; Double d5=82.3751; Double d6=82.275; System.out.println("test:-----------------"+d1+"-----"+d2+"-----"+d3+"------"+d4+"------"+d5+"------"+d6); System.out.println("理想结果:-------------"+82.37+"-----"+82.37+"-----"+82.27+"------"+82.38+"------"+82.38+"------"+82.28); //写法1,结果不不符合 DecimalFormat df = new DecimalFormat("#0.00"); df.setRoundingMode(RoundingMode.HALF_UP); System.out.println("RoundingMode.HALF_UP:--"+df.format(d1)+"-----"+df.format(d2)+"-----"+df.format(d3)+"------"+df.format(d4)+"------"+df.format(d5)+"------"+df.format(d6)); //写法2,结果不不符合 DecimalFormat df2 = new DecimalFormat("#0.00"); df2.setRoundingMode(RoundingMode.HALF_EVEN);//RoundingMode.HALF_EVEN 为默认 System.out.println("RoundingMode.HALF_EVEN:"+df2.format(d1)+"-----"+df2.format(d2)+"-----"+df2.format(d3)+"------"+df2.format(d4)+"------"+df2.format(d5)+"------"+df2.format(d6)); //写法3,结果不不符合 DecimalFormat df3 = new DecimalFormat("#0.00"); df3.setRoundingMode(RoundingMode.UP); System.out.println("RoundingMode.UP:-------"+df3.format(d1)+"-----"+df3.format(d2)+"-----"+df3.format(d3)+"------"+df3.format(d4)+"------"+df3.format(d5)+"------"+df3.format(d6)); //写法4,结果不不符合 DecimalFormat df4 = new DecimalFormat("#0.00"); df4.setRoundingMode(RoundingMode.FLOOR); System.out.println("RoundingMode.FLOOR:----"+df4.format(d1)+"-----"+df4.format(d2)+"-----"+df4.format(d3)+"------"+df4.format(d4)+"------"+df4.format(d5)+"------"+df4.format(d6)); //RoundingMode 下的其余 DOWN,CEILING,HALF_DOWN,UNNECESSARY 82.3650 不做test NumberFormat nf = NumberFormat.getNumberInstance(); // 保留两位小数 nf.setMaximumFractionDigits(2); // 如果不需要四舍五入,可以使用RoundingMode.DOWN nf.setRoundingMode(RoundingMode.UP); System.out.println("NumberFormat format:---"+nf.format(d1)+"-----"+nf.format(d2)+"-----"+nf.format(d3)+"------"+nf.format(d4)+"------"+nf.format(d5)+"------"+nf.format(d6)); //BigDecimal divide RoundingMode.UP System.out.println("BigDecimal divide:-----"+fornum(d1,1,2)+"-----"+fornum(d2,1,2)+"-----"+fornum(d3,1,2)+"------"+fornum(d4,1,2)+"------"+fornum(d5,1,2)+"------"+fornum(d6,1,2)); //Math.round System.out.println("Math.round:------------"+formatDouble1(d1)+"-----"+formatDouble1(d2)+"-----"+formatDouble1(d3)+"------"+formatDouble1(d4)+"------"+formatDouble1(d5)+"------"+formatDouble1(d6)); } //scale 保留小数位 public static Double fornum(Double v1, int v2, int scale) { if (v2 != 0) { BigDecimal b1 = new BigDecimal(v1); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.divide(b2,scale,RoundingMode.UP).doubleValue(); } else { return 0.00; } } public static double formatDouble1(double d) { return (double)Math.round(d*100)/100; } }
结果:test:------------82.365-----82.3651-----82.265------82.375------82.3751------82.275 理想结果:--------------82.37-----82.37-----82.27------82.38------82.38------82.28 RoundingMode.HALF_UP:--82.36-----82.37-----82.27------82.38------82.38------82.28 RoundingMode.HALF_EVEN:82.36-----82.37-----82.27------82.38------82.38------82.28 RoundingMode.UP:-------82.37-----82.37-----82.27------82.38------82.38------82.28 RoundingMode.FLOOR:----82.36-----82.36-----82.26------82.37------82.37------82.27 NumberFormat format:---82.37-----82.37-----82.27------82.38------82.38------82.28-T BigDecimal divide:-----82.37-----82.37-----82.27------82.38------82.38------82.28-T Math.round:------------82.37-----82.37-----82.27------82.38------82.38------82.28-T