Solved

# Problem with DecimalFormat

Posted on 1998-07-26
290 Views
I have this odd problem with DecimalFormat

...
private DecimalFormat f = new DecimalFormat("00.00");

public void paint(Graphics g)
{
g.drawString(f.format(1.15),10,30);
g.drawString(f.format(1.20),10,40);
g.drawString(f.format(1.50),10,50);
}
...

Above draws "01,14", "01,20" and "01,50" on the applet ???
As you can see the first drawString is wrong (I am missing 0.01), if I just draw the numbers without the format it works fine?

I have tryed with other numbers but it is only when I try to draw the figures from 1.13 to 1.15 I have this problem.
0
Question by:usk
• 4
• 2

Author Comment

ID: 1228322
Edited text of question
0

LVL 2

Accepted Solution

threshold earned 50 total points
ID: 1228323
Try this code below:

DecimalFormat f = new DecimalFormat("00.00");
double a=1.15;
System.out.println(f.format(a));

you will get 1.14... It's the general problem with double/float calculation.

The problem is in the statement "double a=1.15"
In fact, a is 1.149999999999... in internal process!
So, the '0.0099999999...' will be truncated by DecimalFormat.
I don't know whether it is bug or not in Java.

Here is two way to fix this 'bug??':
1. if you want to get the 'flood' of the double value:
double p=0.000000000001d;
String s=f.format(a+p);
2. if you want to get the 'neighborhood' of the double value with the foramt '00.00' :
double p=0.000000000001d;
String s=f.format(a+0.005d-p);

hope it's useful.
0

LVL 2

Expert Comment

ID: 1228324
sorry, my English is not good...

There are 2 ways to fix this 'bug??'....
0

Author Comment

ID: 1228325
This must be a bug in the DecimalFormat, as this problem only occurs when using DecimalFormat, and the problem only occures with figures in the range from 1,13 to 1,15.

Your first solution seems useable for my applet, but is it possible that I will get new problems in another range of figures when adding 0.000000000001d ? (will other figures than above will be rounded up) ?
0

LVL 2

Expert Comment

ID: 1228326
All the double/float numbers will be stored as binary data in byte code.
There is no binary data to present as 1.15 exactly. System just chooses the most near data(1.14999999...) to be 1.15.
Because there is no difference between 1.149999.... and 1.15, DecimalFormat can't know that the 1.1499999... is from 1.15 originally.
Of cause, It will happen in any possible numbers and any System(including java,c++,vb...).
I think it's not a bug in Java, It's a 'problem' in Programming for double/float calculation.

For your case, you can use the code simply:
String s=f.format(a+0.005d);  // a is any double
It's can get the neighborhood of 'a' for display.

Or You can add a suitable precision (p=0.00000001) for any double when you display it.

Maybe, you should use java.math.BigDecimal, if you don't care the slow speed, it's a number format for any precision.
0

LVL 2

Expert Comment

ID: 1228327
If you think that to theat 2.99999999 as 3.0 is OK, you can just add the precision factor (p=0.00000001) to the variable when you display it or transfer it into interger. That's why I call the 'p' as precision factor.
And, when you calculate with double, you don't need to add p into it.
Then, It effects the printing of variable only. There won't  be any other problems generated by it.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

rhino JavaScript import, load 25 82
MySQL  on Tomcat 8 43
Why my table column Id is not passed to java object? 4 38
What's wrong with this code? 4 21
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.