Simple one: How would you find the fractional part of a floating point number in PHP? For example, if I have the value 1.25, I want to return 0.25.
A:
My PHP skills are lacking but you could minus the result of a floor from the original number
Ethan Gunderson
2008-09-08 22:01:27
+2
A:
If if the number is negative, you'll have to do this:
$x = abs($x) - floor(abs($x));
yjerem
2008-09-08 22:08:22
+1
A:
Don't forget that you can't trust floating point arithmetic to be 100% accurate. If you're concerned about this, you'll want to look into the BCMath Arbitrary Precision Mathematics functions.
$x = 22.732423423423432;
$x = bcsub(abs($x),floor(abs($x)),20);
You could also hack on the string yourself
$x = 22.732423423423432;
$x = strstr ( $x, '.' );
Alan Storm
2008-09-09 13:38:55