java四舍五入保留两位小数方法整理

转自于: http://blog.csdn.net/he20101020/article/details/14163973

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class test {
	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))); 


	}
}

你可能感兴趣的:(java)