1.

How to add leading zeros in Java?

Answer»

We can add leading zeros to a number in JAVA. LET us see how:

The number we are taking as an example:

15

We will add 6 leading zeros to the above number using the following code. Here, we are working on the String.format() method to achieve the same:

import java.util.Formatter; PUBLIC CLASS Example {    public static VOID main(String args[]) {      int a = 15;      System.out.println("Value = "+a);      // adding leading zeros      String res = String.format("%08d", a);      System.out.println("Updated = " + res);    } }

The output:

Value = 15 Updated = 00000015


Discussion

No Comment Found