I wrote a function to add commas and zeros to a number if necessary, but I've gotten stuck at the modulus function. According to my PHP:
float(877.5) % 1 == 0 //true
Shouldn't 877.5 % 1 == 0.5
?
Modulo is an integer operator, so it converts both the operands to integers before calculating the remainder. So, basically, modulo does integer division and then gives back whatever is left from the dividend. The sign of the value returned by a modulo operation is determined by the sign of the dividend.
The fmod() function returns the remainder (modulo) of x/y.
In most programming languages, modulo is indicated with a percent sign. For example, "4 mod 2" or "4%2" returns 0, because 2 divides into 4 perfectly, without a remainder. "5%2", however, returns 1 because 1 is the remainder of 5 divided by 2 (2 divides into 5 2 times, with 1 left over).
modulo doesn't work with decimals because it only accepts integers. As discussed above, you can define another procedure to see if two numbers are divisible by each other.
It's giving you the reminder of the division what you need is fmod,
fmod — Returns the floating point remainder (modulo) of the division of the arguments
echo fmod(877.5, 1); // 0.5
No, the modulus operator tells you the remainder of the division. Anything divided by 1 does not have a remainder, so it yields 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With