Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modulus division returns an integer?

Tags:

php

Does modulus division only return integers? I need a float return. See the following:

var_dump(12 % 10); // returns 2, as expected
var_dump(11.5 % 10); // returns 1 instead of 1.5?
like image 936
StackOverflowNewbie Avatar asked Dec 12 '22 02:12

StackOverflowNewbie


2 Answers

Yes. the % operator returns an integer.

If you want a floating point result, use the fmod() function instead.

like image 184
earth_tom Avatar answered Dec 14 '22 15:12

earth_tom


See the manual.

Operands of modulus are converted to integers (by stripping the decimal part) before processing.

11.5 becomes 11.

11 % 10 = 1 remainder **1**

Your solution: fmod(), as tom_yes_tom suggests.

like image 43
ccKep Avatar answered Dec 14 '22 15:12

ccKep