tags:

views:

1535

answers:

4

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.

+2  A: 
$x = $x - floor($x)
nlucaroni
A: 

My PHP skills are lacking but you could minus the result of a floor from the original number

Ethan Gunderson
+2  A: 

If if the number is negative, you'll have to do this:

 $x = abs($x) - floor(abs($x));
yjerem
+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