Solved

Double number Rounding issue

Posted on 2004-03-24
6
178 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 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

Independent Software Vendors: 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

Let's recap what we learned from yesterday's Skyport Systems webinar.
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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…
Suggested Courses

617 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