Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Double number Rounding issue

Posted on 2004-03-24
6
Medium Priority
?
181 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
[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
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
rrz earned 100 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 100 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 28

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Are you an Exchange administrator employed with an organization? And, have you encountered a corrupt Exchange database due to which you are not able to open its EDB file. This article will explain all the steps to repair corrupt Exchange database.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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