aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 24166
    
  30

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: 13868
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sinking in float
 
Similar Threads
Changes in the out put
why can a long assigned to float without casting
ints, floats and doubles
Assignement in java
int -> float -> int and loss of precision