File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sinking in float" Watch "Sinking in float" New topic

Sinking in float

David Coulthard

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

Joined: Jul 08, 2003
Posts: 24199

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: 15081

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
I agree. Here's the link:
subject: Sinking in float
It's not a secret anymore!