librin.so.1
librin.so.1

Reputation: 375

empty double and float in printf and String.format

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.

Upvotes: 2

Views: 562

Answers (2)

Piotr Praszmo
Piotr Praszmo

Reputation: 18320

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.

Upvotes: 3

user1201210
user1201210

Reputation: 3809

Here is my code and my output. I don't see a problem.

package foo;

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    double d = 123.456; 
    System.out.println("in println: " + d + " " + (int) d);
    System.out.printf("in printf as int: %d%n", (int) d);
    System.out.printf("in printf with %%f: %a%n", d);
    System.out.printf("in printf with %%e: %e%n", d);
    System.out.printf("in printf with %%g: %g%n", d);
    System.out.printf("in printf with %%a: %a%n", d);

  }

 }

Gives

in println: 123.456 123
in printf as int: 123
in printf with %f: 0x1.edd2f1a9fbe77p6
in printf with %e: 1.234560e+02
in printf with %g: 123.456
in printf with %a: 0x1.edd2f1a9fbe77p6

Upvotes: 2

Related Questions