• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

double + double =?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The following code gives me result as
d is : 298.02049999999997
Shudnt it be 298.0205???

String a="200.0";
String b="98.0205";
double d= Double.parseDouble(a)+Double.parseDouble(b);
System.out.println("d is : "+d);
Thanks in advance!
Kartik
 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to get that answer you will have to format the answer. Double with out formatting will give you the answer like that (default).
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is wierd. I am not sure I understand Jame's reply. Why would 1.0 + .05 = 1.0499999 regardless of formatting?
 
Kartik Ruppa
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you tell me how to format??
Thanks!
Kartik
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the java.text.NumberFormat class. Here is one example. You can also format numbers as percentages, currency, etc.

Output should display : 1.23
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kim Kantola:
That is wierd. I am not sure I understand Jame's reply. Why would 1.0 + .05 = 1.0499999 regardless of formatting?


Because of rounding. The computer thinks in binary, not decimal. The closest binary approximation to 1.05 is not exact so the computer rounds it to the nearest binary equivalent; the result is the 1.49999... number.
Warren
 
James Chegwidden
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would actually use the DecimalFormat class for formatting simple numbers (non money). Most beginning texts use this class rather than NumberFormat class.
reply
    Bookmark Topic Watch Topic
  • New Topic