2012-03-04 92 views
0

我在Wicket中的格式數字有一些問題。在textfiled中只能輸入五位數字 - (xxxxx)如果我把小於五位數字的數字設置爲10,它可以通過零自動將00010等格式更改爲零。我該怎麼辦?謝謝你的建議。在門票中添加0到數字

回答

1
public static String leadingZeros(int value, int returnSize){ 
    int size = (int) Math.log10(value)+1; 
    if (size > returnSize){ 
     return String.valueOf(value).substring(size-returnSize); 
    } 
    StringBuilder string = new StringBuilder(); 
    for (int i=returnSize; i>size;i--){ 
     string.append("0"); 
    } 
    string.append(value); 
    return string.toString(); 
} 

實施例:

System.out.println(leadingZeros(123,5)); 
--> 0

System.out.println(leadingZeros(123456789,5)); 
--> 56789