Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with DecimalFormat

Posted on 1998-07-26
6
Medium Priority
?
303 Views
Last Modified: 2010-03-30
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
Comment
Question by:usk
  • 4
  • 2
6 Comments
 

Author Comment

by:usk
ID: 1228322
Edited text of question
0
 
LVL 2

Accepted Solution

by:
threshold earned 150 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);
You can adjust the precision 'p' for your purpose.

hope it's useful.
0
 
LVL 2

Expert Comment

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

There are 2 ways to fix this 'bug??'....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:usk
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

by:threshold
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

by:threshold
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question