In Java, when I supply a floating point variable to printf or String.format, it gives nothing. To illustrate, an example:
public class test {
public static void main(String arg[]) {
double d = 123.456;
System.out.println("println: " + d + " " + (int) d);
System.out.printf("printf as int: %d%n", (int) d);
System.out.printf("printf with %%f: %f%n", d);
System.out.printf("printf with %%e: %e%n", d);
System.out.printf("printf with %%g: %g%n", d);
System.out.printf("printf with %%a: %a%n", d);
}
}
gives
in println: 123.456 123
in printf as int: 123
in printf with %f:
in printf with %e:
in printf with %g:
in printf with %a:
as You can see, it does work with println, but it fails on printf. Same goes with String.format. If I supply width and precision, it does the same – gives nothing.
What am I doing wrong?
EDIT:
I am using en_US.UTF-8 locale;
I am not sure what version of Java I am using. What I know is: I compile it with gcc (gcj), version 4.7.2; And Have gcj JRE version 4.7.2 and OpenJDK JRE version 7u7-2.3.2a installed.;
Edited the code. Now it is the whole code of the program. I wrote it as a small test program after I noticed these problems with floating point numbers in my other code.
There is a good chance it’s a bug in GCJ. See here: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=561685
Get rid of GCJ and use OpenJDK or Sun/OracleJDK.