Given, say, 1.25 - how do I get \"1\" and .\"25\" parts of this number?
I need to check if the decimal part is .0, .25, .5, or .75.
a short way (use floor and fmod)
$var = "1.25";
$whole = floor($var); // 1
$decimal = fmod($var, 1); //0.25
then compare $decimal to 0, .25, .5, or .75
Cast it as an int and subtract
$integer = (int)$your_number;
$decimal = $your_number - $integer;
Or just to get the decimal for comparison
$decimal = $your_number - (int)$your_number
To prevent the extra float decimal (i.e. 50.85 - 50 give 0.850000000852), in my case I just need 2 decimals for money cents.
$n = 50.85;
$whole = intval($n);
$fraction = $n * 100 % 100;
Just to be different :)
list($whole, $decimal) = sscanf(1.5, '%d.%d');
CodePad.
As an added benefit, it will only split where both sides consist of digits.
$x = 1.24
$result = $x - floor($x);
echo $result; // .24
If you can count on it always having 2 decimal places, you can just use a string operation:
$decimal = 1.25;
substr($decimal,-2); // returns "25" as a string
No idea of performance but for my simple case this was much better...