What is the Java equivalent of printf% g C format specifier?

I tried using Formatter.format, but it looks like it left the mantissa on numbers with 0 mantissa, while the C version is not. Is there an equivalent to the C% g format specifier in Java, and if not, is there a way to fake it? My intention is to keep the mantissa exactly the same as for compatibility reasons.

foo.c

#include <stdio.h> int main (int argc, char const *argv[]) { printf("%g\n", 1.0); return 0; } 

Main.java

 class Main { public static void main(String[] args) { System.out.printf("%g\n", 1.0); } } 

Console:

 $ javac Main.java && java Main 1.00000 $ gcc foo.c && ./a.out 1 

Similarly, with input 1.2, the mantissa is longer in the Java version

 $ javac Main.java && java Main 1.20000 $ gcc foo.c && ./a.out 1.2 
+6
java c printf formatter
source share
5 answers

Have you tried the java.text.DecimalFormat class ?

 System.out.println(new DecimalFormat().format(1.0)); 

outputs:

 1 

then:

 System.out.println(new DecimalFormat().format(1.2)); 

outputs:

 1.2 
+2
source share

edit : This code causes the fractional part to be skipped if the exponent is 17 digits, due to my misunderstanding of how String.format formatted these numbers. So please do not use this code: P

Thanks for the entry guys. I couldn't find a way to configure DecimalFormat or NumberFormat to clone functions exactly, but this method seems to work (followed by an example):

 String.format("%.17g", x).replaceFirst("\\.?0+(e|$)", "$1"); 

main.c

 #include <stdio.h> int main (int argc, char const *argv[]) { printf("%.*g\n", 17, -0.0); printf("%.*g\n", 17, 0.0); printf("%.*g\n", 17, 1.0); printf("%.*g\n", 17, 1.2); printf("%.*g\n", 17, 0.0000000123456789); printf("%.*g\n", 17, 1234567890000000.0); printf("%.*g\n", 17, 0.0000000123456789012345678); printf("%.*g\n", 17, 1234567890123456780000000.0); return 0; } 

Main.java

 class Main { public static String formatDouble(double x) { return String.format("%.17g", x).replaceFirst("\\.?0+(e|$)", "$1"); } public static void main(String[] args) { System.out.println(formatDouble(-0.0)); System.out.println(formatDouble(0.0)); System.out.println(formatDouble(1.0)); System.out.println(formatDouble(1.2)); System.out.println(formatDouble(0.0000000123456789)); System.out.println(formatDouble(1234567890000000.0)); System.out.println(formatDouble(0.0000000123456789012345678)); System.out.println(formatDouble(1234567890123456780000000.0)); } } 

and their outputs:

  $ gcc foo.c && ./a.out
 -0
 0
 one
 1.2
 1.23456789e-08
 1234567890000000
 1.2345678901234567e-08
 1.2345678901234568e + 24
 $ javac Main.java && java Main
 -0
 0
 one
 1.2
 1.23456789e-08
 1234567890000000
 1.2345678901234567e-08
 1.2345678901234568e + 24
+4
source share

You can use NumberFormat . By setting the minimum digits of the digits to 0, but leaving the maximum size, it should do what you want.

It is not as simple as printf, but it should work.

+1
source share

well, you can specify how many digits you want: "% .2g" will display 1.2 as 1.20

0
source share

Please note that use

 String.format("%.17g", x).replaceFirst("\\.?0+(e|$)", "$1") 

does not work for 1.2345e-20 (aligns zero from the end, producing 1.2345e-2). Here is what I used in my code:

 String.format(Locale.US, "%.6g", x).replaceFirst("\\.0+(e|$)", "$1").replaceFirst("(\\.[0-9]*[1-9])(0+)(e|$)", "$1$3") 

Basically, I changed the expression to remove only zeros at the end of the fractional part and split it into two cases. But thanks for pointing me in the right direction.

0
source share

All Articles