javaleading-zero

How to left-pad double with zeroes in Java


I need to pre-pend a number of zeroes to a double value in Java. It is simple with integers, for example:

System.out.printf("%08d\n", 1);

Produces: 00000001

If i try the same with a real value:

System.out.printf("%08f\n", 1.1);

The produced output is 1.100000

How do i achieve 0000001.1 ?

Thanks.


Solution

  • Try this

    System.out.println(String.format("%07.1f",1.1F));