java正则去掉小数点后多余0

http://blog.sina.com.cn/s/blog_4adc4b090101i93n.html
需求:已知字符串为一数字字符形式,多为float,double转换过来,将其后多余的0与.去掉.
package test;
public class TestString {
public static void main(String[] args) {
Float f = 1f;
System.out.println(f.toString());//1.0
System.out.println(subZeroAndDot("1"));; // 转换后为1
System.out.println(subZeroAndDot("10"));; // 转换后为10
System.out.println(subZeroAndDot("1.0"));; // 转换后为1
System.out.println(subZeroAndDot("1.010"));; // 转换后为1.01
System.out.println(subZeroAndDot("1.01"));; // 转换后为1.01
}
public static String subZeroAndDot(String s){
if(s.indexOf(".") > 0){
s = s.replaceAll("0+?$", "");//去掉多余的0
s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
}
return s;
}
}

猜你喜欢

转载自panyongzheng.iteye.com/blog/2228252