Solved

Problem with DecimalFormat

Posted on 1998-07-26
6
288 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now