去掉数字前的0:
String a = "000389";
String b = a.replaceAll("0*", "");
System.out.println(b);去掉小数点后多余的0,若小数点后无数字去掉小数点:
public static void main(String[] args){
System.out.println(formatData("2.3400"));
System.out.println(formatData("2.00"));
}
public static String formatData(String s){
if(s.indexOf(".")>0){
s = s.replaceAll("0+?$", "");
s = s.replaceAll("[.]$", "");
}
return s;
}

本文介绍了两种实用的Java方法来处理字符串中的多余0:一种是去除数字前导0,另一种是去掉小数点后的多余0,并在必要时移除小数点。这两种方法对于数据格式化特别有用。
806

被折叠的 条评论
为什么被折叠?



