Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Sinking in float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sinking in float" Watch "Sinking in float" New topic
Author

Sinking in float

David Coulthard
Greenhorn

Joined: Apr 16, 2008
Posts: 16
Hi all,

Im extracting numeric data from a data base. I add all these data up and store it in an integer called totalPlan. Then I store this integer value in a float called planTotal. When i do that, sometimes i find that the int and float differ in the last digit.

for example,

if int totalPlan = 59879301 and i store this in float variable planTotal, then it stores planTotal as 5.98793E7 which when i display gets translated to 59879300.00.

Is there anyway I can avoid this loss of precision ? also, what causes this loss ? thanks in advance.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Read this.
[ September 26, 2008: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Floating-point numbers are not infinitely precise. A float in Java has approximately 6 or 7 digits of precision.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
wood burning stoves
 
subject: Sinking in float