I'm using jsdk 1.5 beta 2. I think there is a bug in the new formatted
output and was wondering if anyone knows if it has been fixed in the
newer beta 3 builds. Note that the output of 0.01 shows up as 0.10
program:
public class TestFormatting {
public static void main(String [] args){
System.out.format("%.2f\n",1.0);
System.out.format("%.2f\n",0.1);
System.out.format("%.2f\n",0.01);
}
}
output:
$ java -cp . TestFormatting
1.00
0.10
0.10
thanks
Here's what I found in the java.sun.com bug database. It seems odd
that this bug was first reported in February but is still reported as
unfixed in 1.5 beta 3 snapshot 55 (18 june 04).
Bug ID: 5002937
Votes 0
Synopsis (fmt) formatting for small floats losing exponent and
rounding incorrectly
Category java:classes_util
Reported Against 1.5 , tiger-beta , tiger-beta2
Release Fixed tiger-rc
State Closed, fixed
Related Bugs 5005649 , 5086406
Submit Date 25-FEB-2004
Description This code:
class PrintfEks {
public static void main(String[] args) {
double number = 8.5;
String format = "%9.2f\n";
System.out.print("Format string: "+format);
for (int i = 0; i < 8; i++) {
System.out.print("(Number: "+number+") ");
System.out.printf(format, number);
number /= 10;
}
number = 0.085;
System.out.print("(Number: "+number+") ");
System.out.printf("%9.2f\n", number);
}
}
produces the following
Format string: %9.2f
(Number: 8.5) 8.50
(Number: 0.85) 0.85
(Number: 0.08499999999999999) 0.84
(Number: 0.008499999999999999) 0.84
(Number: 8.499999999999998E-4) 0.84
(Number: 8.499999999999998E-5) 0.84
(Number: 8.499999999999998E-6) 0.84
(Number: 8.499999999999998E-7) 0.84
(Number: 0.085) 0.85
completely wrong for number<0.85
xxxxx@xxxxx 2004-02-25
Work Around None
Evaluation Oh my!
This needs to be fixed as soon as possible.
-- xxxxx@xxxxx 2004-02-25
Comments
Include a link with my name & email
Submitted On 04-JUN-2004
foxapple
Still exists in JDK 1.5 Beta2
Submitted On 18-JUN-2004
cayhorstmann
Still wrong in beta 3 snapshot 55.
public class Test
{
public static void main(String[] args)
{
System.out.printf("%8.2f%n", 0.01); // prints 0.1
}
}
Submitted On 19-JUL-2004
Matei
Also happens with plain %f;
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
System.out.printf("%f%n", sin(PI)); // prints out 0.122464
}
}