on-internet
← Community

Why does performing modulo on a float not give the expected result?

Stack Overflow ·

Can someone give me an explanation on why this is happening in PHP: echo (0.29*100)%100 // result 28 It's probably very simple but logically I do not see any explanation. Probably how PHP works in the background. I was trying to get the first two decimal positions of a number when I ran into this case. Result should be 29 naturally. If I round the multiplication the result is fine: echo (round(0.29*100))%100 // result 29

Comments

Sign in or sign up to comment and upvote.

No comments yet.