package test;
/**
* 去掉多餘的.與0
* @author Hust
* @Time 2011-11-7
*/
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
}
/**
* 使用java正則表達式去掉多餘的.與0
* @param s
* @return
*/
public static String subZeroAndDot(String s){
if(s.indexOf(".") > 0){
s = s.replaceAll("0+?$", "");//去掉多餘的0
s = s.replaceAll("[.]$", "");//如最後一位是.則去掉
}
return s;
}
}