Solved

Problem with DecimalFormat

Posted on 1998-07-26
6
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

751 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