Solved

Double number Rounding issue

Posted on 2004-03-24
6
167 Views
Last Modified: 2010-04-01
Hi Experts,

Look at the simple example

double a = 1440.0;
double b = 0.06;
double c = a * b;

out.println("Result " + c)  gives 86.39999999999999;

Where
out.println("Result " + 1440.0 * 0.06) gives exact value 86.4   which is correct;

Does anybody knows why this happens, and how to fix a*b -> 86.4


Thanks
Britto
0
Comment
Question by:de_britto
  • 2
6 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 25 total points
ID: 10672181
>out.println("Result " + 1440.0 * 0.06) gives exact value 86.4   which is correct;
I don't get that result on my Tomcat 5 and  JDK1.4 .  I get   86.39999999999999  

>how to fix a*b -> 86.4
Here is one way.  

<%
float a = 1440.0f;
float b = 0.06f;
float c = a * b;
out.println("Float Result = " + c);  
%>
0
 
LVL 92

Assisted Solution

by:objects
objects earned 25 total points
ID: 10674232
Use DecimalFormat class to display to the number of required decuimal places.


DecimalFormat df = new DecimalFormat("#.##");
out.println("Result "+df.format(c));  
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10674601
DecimalFormat format = new DecimalFormat("#.#");
out.println(format.format(c));            
0
 
LVL 27

Expert Comment

by:rrz
ID: 10678566
to objects and mmuruganandam,  
Great, your solutions work for the given values, because DecimalFormat uses half-even rounding  for formatting.  

 "de britto" wanted the  exact value
>gives exact value 86.4   which is correct  

But what if the variables change ?  (  b = 0.061111 for example )  
How could your solution be applied to the general case ?  

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
squirrelPlay java challenge 40 158
sortaSum challenge java 17 171
Retrieving file from bytes array in spring mvc 8 60
iterator/ListIterator approach 17 25
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
SEO can be a real minefield to navigate, but there are three simple ways to up your SEO game just be re-assessing your content output.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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