Big Problem with decimalFormat

Hi,

My first post, and its a good one ...

We are about to go to IST with a rather large reporting application developed in JSP against an Oracle database.

We are using a home grown JDBC based database pooling class.

We are using the decimalFormat class to format the output into whole numbers with commas separating the thousands.

We are using jdk1.2.2 on Solaris 8

The problem I have is that the decimalFormat.format method appears to randomly return incorrect values.

I am using the declaration fmt = new decimalFormat("#,##0") and then simply calling fmt.format(number as a string)

For instance, on one particular report, the there are two values returned from the database, both are '0'

The first number is formatted to 0, but the second is formatted to 1.

If I change the format declaration to "#,##0.0" then I get 0.0 for both numbers.

Even weirder is that if I call the fmt.format() twice on the second variable, then I get return 1 and then 0

Any suggestions ?

Thanks,

Andrew.

[1092 byte] By [awilcockson] at [2007-9-26 2:46:39]
# 1

Does this only happen when the numbers come from the database, or can you replicate the problem with a simple program using constants only? If it's only when the numbers come from the database, I'd suggest that perhaps you are misunderstanding those numbers in some way. Anyway, if you can produce a program (database involved or not) like this:int number = ...something...;

System.out.println(fmt.format(number));

System.out.println(fmt.format(number));

that doesn't print out two duplicate lines, then you have a bug.

(By the way, what or where is IST?)

DrClap at 2007-6-29 10:30:16 > top of Java-index,Archived Forums,Java Programming...
# 2

Hi,

Very hard to replicate outside of the database program.

What I have done is written code that says (effectively)

{

DecimalFormat fmt = new DecimalFormat("#,##0");

ResultSet rs = stmt.executeQuery("select number from table"):

while(rs.next())

{

String num = rs.getString(1);

System.out.println(fmt.format(new Double(num)));

System.out.println(fmt.format(new Double(num)));

}

}

Now with a table with only one column (numeric) and two rows both with a value of zero in the only column, the program would produce

0 <- results from first row

0

1 <- results from second row

0

Oh, and IST is Integrated String Test.

Nobody here knows what it means either :-)

Thanks,

Andrew.

awilcockson at 2007-6-29 10:30:16 > top of Java-index,Archived Forums,Java Programming...
# 3
Did you ever solve this problem? I am having the same problem.
jvdrury at 2007-6-29 10:30:16 > top of Java-index,Archived Forums,Java Programming...
# 4

I was getting this same problem. Including the behavior that the result of the format would change if I called it successive times with the same value. What's strange is that we would call this dozens of times to create a formatted report. The bad values would appear to be randomly distributed throughout the report. However, running successive times always resulted in the bad values being in the same positions. A change that introduced one additional format would change the distribution of the bad values throughout the report.

We were getting this with Solaris JVM, version 1.2.2_05a. After deciding that the code in question was too simple to be the culprit, I tried running on version 1.2.2_7 of the same JVM, which we had on the same machine, and it works correctly everytime!

Unfortunately, we are not in a position to change the version of the JVM we are running on in production, so I needed a workaround. We found that this problem only occurs in .format(Object). Other code that passed native numbers to .format() did not have a problem. We were passing an Integer to format when we were getting the strange results. Here's a simplified snippet:

Integer batchCount = new Integer(0);

//Other code modifies this value...and then...

DecimalFormat reportFormat = new DecimalFormat("000");

System.out.println(reportFormat.format(batchCount)); //Printed a 001

System.out.println(reportFormat.format(batchCount)); //Printed a 000

Since format() can also take a long or a double, I changed the code to pass a long to the format:

Integer batchCount = new Integer(0);

//Other code modifies this value...and then...

DecimalFormat reportFormat = new DecimalFormat("000");

System.out.println(reportFormat.format(batchCount.longValue())); //Printed a 000

System.out.println(reportFormat.format(batchCount.longValue())); //Printed a 000

This solved the problem for us.

Glen

P.S. For those who track this board frequently, sorry to respond to an outdated post. When I was trying to solve this problem, I found this post in a search. While the original poster may never see it, others like myself who get most of their questions answered by searching for answers already given to previous posts will benefit from having a solution.

glenwill at 2007-6-29 10:30:16 > top of Java-index,Archived Forums,Java Programming...