标签:
public static void main(String[] args) { String str="86.64466666"; BigDecimal bd = new BigDecimal(Double.parseDouble(str)); System.out.println(bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()); System.out.println("================="); DecimalFormat df = new DecimalFormat("#.00"); System.out.println(df.format(Double.parseDouble(str))); System.out.println("================="); System.out.println(String.format("%.2f", Double.parseDouble(str))); System.out.println("================="); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(2); System.out.println(nf.format(Double.parseDouble(str))); }
基本数据类型转String的时候可以通过包装类进行自动拆箱
标签:
原文地址:http://www.cnblogs.com/cfb513142804/p/5315717.html