Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Double / Float Doubt.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

this is my code...when i print the values for d3 and d2 , their values are printed with spurious decimal places. why is that ? why not print just zeros after the relevant two decimal places.

System.out.println("addition of129.845+12.84 = " +(129.84+12.84));
double d1 = 129.84;
float f1=12.84f;
double d3=f1;
System.out.println("d1 "+d1);

any help will be appreciated.



System.out.println("d3 =" +d3);
double d2= (d1+f1);
System.out.println("d2 = " +d2);
[ December 21, 2004: Message edited by: down planet ]
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that f1 cannot be represented in a float as 12.84, so Java uses the closest approximation. Then when the float is converted to a double, the extra decimal places result from the slight difference between 12.84 and f1.

If you want to see this, try your code again with "double f1=12.84;".

Another way to handle this is to just show 2 decimal places. You can use the NumberFormat class or, in Java 5, printf() with %.2f. Eiher way, Java will round your output to 2 decimal places.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java uses an IEEE standard to store double and float values. Because of the way this standard works, most floating point values cannot be stored exactly. This is very similar to problems that we see when trying to write fractions as decimal take one-third (1/3) for example. If we write this as a decimal, we have to round off to a certain number of decimal places (ie. 0.33, 0.3333, or 0.333333). Computers have to perform similar rounding when storing floating point values. If you'd like more details on how this works, you should google for "IEEE floating point standard" or use the Search tool here at the Saloon. Others have previously asked questions similar to yours.

Layne
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic