Forums Register Login

decimal part of a float

+Pie Number of slices to send: Send
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 ;-)
+Pie Number of slices to send: Send
float f;
....
float frac = f - (int)f;
+Pie Number of slices to send: Send
thanks
+Pie Number of slices to send: Send
 

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);
+Pie Number of slices to send: Send
But that works properly only for negative numbers...
[ October 14, 2002: Message edited by: Ron Newman ]
+Pie Number of slices to send: Send
Of course I meant *floor*, not ceil... :roll:
+Pie Number of slices to send: Send
Or float % 1.
+Pie Number of slices to send: Send
 

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.
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12529 times.
Similar Threads
All Vowels in an Integer
narrowing primitive casts
assiging int to float
SetPrecision?
getting numberformatexception For input string: "500.0"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:51:36.