The format() method of the String class is used to convert string to integer with leading zeros.
Example:
package com.w3spoint;
public class StringToInt {
public static void main(String args[]){
String str="000000575";
/* String to int conversion with leading zeroes
* the %06 format specifier is used to have 6 digits in
* the number, this ensures the leading zeroes
*/
str = String.format("%06d", Integer.parseInt(str));
System.out.println("Output String: "+str);
}
} |
package com.w3spoint; public class StringToInt {
public static void main(String args[]){
String str="000000575";
/* String to int conversion with leading zeroes
* the %06 format specifier is used to have 6 digits in
* the number, this ensures the leading zeroes
*/
str = String.format("%06d", Integer.parseInt(str));
System.out.println("Output String: "+str);
}
}
Output:
Download this example.