Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# decimal part of a float

fab etore
Greenhorn
Posts: 9
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.

Ron Newman
Ranch Hand
Posts: 1056
float f;
....
float frac = f - (int)f;

fab etore
Greenhorn
Posts: 9
thanks

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Ron Newman:
float f;
....
float frac = f - (int)f;

That doesn't work well for f > Integer.MAX_VALUE.
float frag = f - Math.ceil(f);

Ron Newman
Ranch Hand
Posts: 1056
But that works properly only for negative numbers...
[ October 14, 2002: Message edited by: Ron Newman ]

Ilja Preuss
author
Sheriff
Posts: 14112
Of course I meant *floor*, not ceil... :roll:

Dirk Schreckmann
Sheriff
Posts: 7023
Or float % 1.

Ilja Preuss
author
Sheriff
Posts: 14112
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.