当我们输出的小数不知道有几位小数,也不知道后面有没有带零,去掉后面多余零可以采用以下方法。在实际使用中,多用于小数转百分数,百分数前面的小数乘以100后转String输出,输出的String很多带零,可以用此方法先去掉小数多余的零再转String。

 1         DecimalFormat decimalFormat = new DecimalFormat("###.##");
 2         float a = 0.3450f;
 3         float b = 0.0123f;
 4         float c = 3.001234f;
 5         String d=decimalFormat.format(a);
 6         String e=decimalFormat.format(b);
 7         String f=decimalFormat.format(c);
 8         System.out.println(d);//0.34
 9         System.out.println(e);//0.01
10         System.out.println(f);//3    

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案