Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sinking in float

 
David Coulthard
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this.
[ September 26, 2008: Message edited by: Ernest Friedman-Hill ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating-point numbers are not infinitely precise. A float in Java has approximately 6 or 7 digits of precision.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic