java/android: 保留小数点2位,四舍五入,小于2位原样输出

Talk is cheap , show you the code!!!!

double xx = 1.2222;
		System.out.println("保留两位小数:"+String.format("%.2f",xx));//输出两位,四舍五入
		
		DecimalFormat df1 = new DecimalFormat("#.00");  //输出两位,四舍五入
		DecimalFormat df2 = new DecimalFormat("#.##");  //小于两位原样输出,大于两位,输出两位,四舍五入
		
		String format = df1.format(xx);
		System.out.println(format);

		String format2 = df2.format(xx);
		System.out.println(format2);
  •  xx = 1.2222时,输出如下
  • xx = 1.2时,输出如下
  • xx = 1时,输出如下

猜你喜欢

转载自blog.csdn.net/mawei7510/article/details/87872923