php / php-langspec

PHP Language Specification
http://www.php.net
Other
2.3k stars 271 forks source link

Misleading conversion float to int #242

Closed Tomanhez closed 5 years ago

Tomanhez commented 5 years ago

Screenshot 2019-09-25 at 16 10 00

If you have float var where decimal variable is 05, like this: 33.05 and multiply it by 100 You mistakenly get 3305 but if convert it on int you have 3304

Certainly occurs in php 7.1.25 - 7.4.0

nikic commented 5 years ago

32.05*100 is 3204.9999999999995 due to limited float precision, which is 3204 when rounding down, which is what (int) does (for positive numbers). Use round() if you want to round.