Solved

Double number Rounding issue

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

12 Experts available now in Live!

Get 1:1 Help Now