Tutorialspoint examples

Learn java tutorial with examples for beginners online

java convert string to integer with leading zeros


The format() method of the String class is used to convert string to integer with leading zeros.

Example:

package com.tutorialspointexamples;
 
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:

Output String: 000575

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status