php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77149 Invalid float to integer
Submitted: 2018-11-14 08:45 UTC Modified: 2018-11-17 00:58 UTC
From: antsokol at mail dot ru Assigned:
Status: Not a bug Package: Math related
PHP Version: 7.2.12 OS: Windows 10 64bit
Private report: No CVE-ID: None
 [2018-11-14 08:45 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-14 10:13 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2018-11-14 10:13 UTC] 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-17 00:58 UTC] yohgaki@php.net
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;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 08 03:01:28 2024 UTC