reproduced from: http://ych0108.iteye.com/blog/2174134 p>
Java int String number is not enough to fill in the front zero
String.format("%010d", 25); //25为int型
0 represents the character to be filled before
10 represents the length of the string
and d represents the argument of integer type
today I want to put an int to String not enough digits in front of the zero, in the original to see if there is a ready-made API, the results found most of the following
public static String addZeroForNum(String str,int strLength) {
int strLen =str.length();
if (strLen <strLength) {
while (strLen< strLength) {
StringBuffersb = new StringBuffer();
sb.append("0").append(str);//左补0
// sb.append(str).append("0");//右补0
str= sb.toString();
strLen= str.length();
}
}
return str;
}
but I think it’s a little bit of a hassle, so I thought of a slightly easier way to do it, the following line would be
String str = String.format("%5d", num).replace(" ", "0");
, where num is an int and STR is the converted result. That’s easy.
So,
, and I recently did a search on string.format, which actually comes with its own way to fill in the zero,
String.format("%06",12);//其中0表示补零而不是补空格,6表示至少6位
div>
Read More:
- Java encrypts the string with MD5
- Differences between length() size() and C strlen() of C + + string member functions
- On the intern () method of string class in Java
- Nanny level solutions use the enterprise version of MyEclipse to show the MyEclipse trial expired solution and activation
- JSON data format net.sf.json .JSONException: A JSONObject text must begin with ‘{‘ at character 1 of Error:(f…
- Mybatis custom list collection parser
- JavaScript emoticon package encryption
- SQLServerException: The server failed to resume the transaction. Desc:ab00000002
- [leetcode] zigzag transformation
- Difference between isempty method and isblank method in stringutils
- C / C + + library function (tower / tower) realizes the conversion of letter case
- Register global time filter in main.js in Vue
- Format control of cout cout.width () and cout.fill ()
- C++ string substr()
- Local date time conversion in java8
- A repeated string is composed of two identical strings. For example, abcabc is a repeated string with length of 6, while abcba does not have a duplicate string. Given any string, please help Xiaoqiang find the longest repeated substring.
- How to display the Chinese name and installation time of installed software in snmp4j walk Windows
- High precision addition function template
- How to get all the keys of map by golang
- [Two Sigma OA] Longest Chain