java保留两位小数4种方法
package cn.com.songjy.test;import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;public class FormatDigital {private static double digital = 111231.5585;public static void main(String[] args) {way1();way2();way3();way4();}static void way1(){ BigDecimal bigDecimal = new BigDecimal(digital); System.out.println(bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());}static void way2(){DecimalFormat decimalFormat = new DecimalFormat("#.00");System.out.println(decimalFormat.format(digital));}static void way3(){System.out.println(String.format("%.2f", digital));}static void way4(){NumberFormat numberFormat = NumberFormat.getNumberInstance();numberFormat.setMaximumFractionDigits(2);System.out.println(numberFormat.format(digital));}}