Bug #77149 [Nab]: Invalid float to integer
Edit report at https://bugs.php.net/bug.php?id=77149&edit=1
ID: 77149
Updated by: yohgaki@php.net
Reported by: antsokol at mail dot ru
Summary: Invalid float to integer
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Windows 10 64bit
PHP Version: 7.2.12
Block user comment: N
Private report: N
New Comment:
Python and Ruby converts IEEE 754 float values like PHP. You can use round() to get desired integer
value from IEEE 754 double precision.
i.e.
echo (integer)round($price) . PHP_EOL;
Previous Comments:
------------------------------------------------------------------------
[2018-11-14 10:13:28] bwoebi@php.net
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/
Thank you for your interest in PHP.
------------------------------------------------------------------------
[2018-11-14 08:45:03] antsokol at mail dot ru
Description:
------------
Invalid float to integer conversion
Test script:
---------------
$price = 76.24;
$price *= 100;
echo $price . PHP_EOL; //7624
echo (integer)$price . PHP_EOL; //7623
echo intval($price) . PHP_EOL; //7623
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77149&edit=1
Thread (3 messages)