Solved

Problem with DecimalFormat

Posted on 1998-07-26
6
295 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 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);
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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

685 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