0

I was trying to convert float value to a fixed length string. I got an unexpected output when the format is greater than %.6. Sample code and output are below,

public class Test {

    public static void main(String[] args) {
        float a=10.9f; 
        System.out.println(String.format("%07.2f", a)); 
        System.out.println(String.format("%09.4f", a)); 
        System.out.println(String.format("%011.6f", a)); 
        System.out.println(String.format("%012.7f", a)); 
        System.out.println(String.format("%013.8f", a));   
    }

}

Output

0010.90
0010.9000
0010.900000
0010.8999996
0010.89999962

Why am I getting 8999996 & 89999962 instead of 9000000 & 90000000?

Kajal
  • 709
  • 8
  • 27

0 Answers0