1

Is it possible to format for example a double of value 0.41 to 00.41 using only String format ? In other words, is it possible to left zero pad doubles ? The result I would like should show as xx.xx

Unfortunately I haven't been able to find something simple although I am sure there is a simple solution... I have tried %02.2f but it doesn't work...

Thank you for your help !

beetix
  • 99
  • 9

1 Answers1

2

Try this

public class DoubleFormat {
    public static void main(String[] args) {
        float f =  0.41f;
        String s = Float.toString(f);

        System.out.println("0"+s); //Or
        System.out.printf("%05.2f\n", f);
    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Razib
  • 10,965
  • 11
  • 53
  • 80
  • It won't work with, for example, 14.01 which with your code would give 014.01. I would like two digits before the "." and two after. – beetix May 30 '15 at 18:20