java中百分比和小数的相互转换

  public static void main(String[] args) {
	  //百分比转换为小数
	  String percent="66.60%";
	  percent=percent.replace("%","");
	  Float f = Float.valueOf(percent) / 100;
	  System.out.println(f.toString());//输出结果 0.666
	  
	  //小数转换为百分比
	  String  decimal = "0.83";
	  DecimalFormat df = new DecimalFormat("0.00%");
	  BigDecimal bigDecimal = new BigDecimal(decimal);
	  System.out.println(df.format(bigDecimal));//输出结果 83.00%
}

猜你喜欢

转载自blog.csdn.net/weixin_37942364/article/details/88868414