Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

decimal part of a float

 
fab etore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like to know how can i get the decimal part of a float. I think there must be a method for, by i can't find it.
please help me ;-)
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float f;
....
float frac = f - (int)f;
 
fab etore
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
float f;
....
float frac = f - (int)f;

That doesn't work well for f > Integer.MAX_VALUE.
Instead you should probably use
float frag = f - Math.ceil(f);
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that works properly only for negative numbers...
[ October 14, 2002: Message edited by: Ron Newman ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I meant *floor*, not ceil... :roll:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or float % 1.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
Or float % 1.

Wow, I didn't know that "%" was defined for floats in Java...
That's certainly the most appropriate solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic