浮点数取两位小数

String str;
		do{
			Scanner scanner = new Scanner(System.in);
			System.out.println("请输入年利率数字部分(如年利率为8.5%,则输入8.5即可.)");
			str = scanner.nextLine();
			float fff = (Float.parseFloat(str)*100)/360;
			System.out.println("变形前:>>>>>"+fff+"");
			System.out.println("万份日收益为:Math.round>>>>>"+(double)(Math.round(fff*100)/100.0)+"元");
			System.out.println("万份日收益为:format>>>>>"+String.format("%.2f",fff));
			DecimalFormat df = new DecimalFormat("#.##");
			System.out.println("万份日收益为:DecimalFormat>>>>>"+Double.parseDouble(df.format(fff))+"    之前>>>>>"+df.format(fff));
			System.out.println("万份日收益为:double>parseString>>>>>"+Double.parseDouble(String.format("%.2f", fff)));
			BigDecimal bd = new BigDecimal(fff);
			BigDecimal bd2 = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
			System.err.println("1111万份日收益为:BigDecimal>>>>>"+bd2.toString()+"   Double.parse>>>>>"+Double.parseDouble(bd2.toString()));
		}while(true);
原文地址:https://www.cnblogs.com/lowerCaseK/p/SetScale.html