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:
- 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.
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- After Java application is deployed in Linux environment, Chinese is displayed as square solution
- TSLint:object access via string literals is disallowedtslint(no-string-literal)
- Split keyword in ABAP when the separator is at the beginning and end of the string
- The JSON object is converted into a formdata object, and the formdata object is converted into a JSON object
- Object is JSON, and JSON is converted to object
- Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0
- After Vue is packaged, a blank page appears, an error is reported in the resource request, and there is no error handling scheme
- Python TypeError: not all arguments converted during string formatting [Solved]
- zookeeper Failed to Startup: Error: JAVA_HOME is not set and java could not be found in PATH
- An import error is reported in the python. The solution to setting. Pylintrc is invalid
- After node.js is installed, use the instruction node version in vscode to show that it is not an external or internal instruction. The solution is as follows:
- TypeError: ‘numpy.int64′ object is not iterable ,’int’ object is not iterable
- Java encrypts the string with MD5
- Python: How to Fix “Ord() expected string of length 1, but int found”
- A solution to the problem that the number of nodes does not increase and the name of nodes is unstable after adding nodes dynamically in Hadoop cluster
- Keil :error: identifier “int8_ T “is undefined
- Keil : error: identifier “int8_ T “is undefined
- An error is reported when the file in hive parquet format is written in the Flink connection