public classTest{?
?public static void main(String[] args) {
? ? ? ? double d = 756.2345566;
? ? ? ? //方法一:最簡(jiǎn)便的方法预麸,調(diào)用DecimalFormat類? ? ? ? DecimalFormat df = new DecimalFormat(".00");
? ? ? ? System.out.println(df.format(d));
? ? ? ? //方法二:直接通過(guò)String類的format函數(shù)實(shí)現(xiàn)? ? ? ? System.out.println(String.format("%.2f", d));
? ? ? ? //方法三:通過(guò)BigDecimal類實(shí)現(xiàn)? ? ? ? BigDecimal bg = new BigDecimal(d);
? ? ? ? double d3 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
? ? ? ? System.out.println(d3);
? ? ? ? //方法四:通過(guò)NumberFormat類實(shí)現(xiàn)? ? ? ? NumberFormat nf = NumberFormat.getNumberInstance();
? ? ? ? nf.setMaximumFractionDigits(2);
? ? ? ? System.out.println(nf.format(d));
? ? }
}
轉(zhuǎn)載自https://blog.csdn.net/csdnlijingran/article/details/82179248