Liftoff
Liftoff

Reputation: 25392

PHP float modulus not working

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?

Upvotes: 19

Views: 7577

Answers (2)

Rikesh
Rikesh

Reputation: 26441

It's giving you the reminder of the division what you need is fmod,

fmodReturns the floating point remainder (modulo) of the division of the arguments

echo fmod(877.5, 1); // 0.5

Upvotes: 25

Sterling Archer
Sterling Archer

Reputation: 22405

No, the modulus operator tells you the remainder of the division. Anything divided by 1 does not have a remainder, so it yields 0.

Upvotes: 2

Related Questions