php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76105 String to Number conversions
Submitted: 2018-03-16 13:04 UTC Modified: 2018-03-16 14:34 UTC
From: mail at fsax dot ch Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
 [2018-03-16 13:04 UTC] mail at fsax dot ch
Description:
------------
Some String to Number conversions create unexpected results that can lead to bad miscalculations.

On the php.net docs i found a warning:
"Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results."

but i think the root of my issue manifests while the float is beeing cast, since the floor() has the same result.

Test script:
---------------
echo ("9312.80" *100)."<br>\n";
echo (((float) "9312.80") *100)."<br>\n";
echo (int) ("9312.80" *100)."<br>\n";
echo floor( "9312.80" *100);


Expected result:
----------------
237280<br>
237280<br>
237280<br>
237280

Actual result:
--------------
237280<br>
237280<br>
931279<br>
931279

Patches

itsAGeneralIssue (last revision 2018-03-16 13:06 UTC by mail at fsax dot ch)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-03-16 14:34 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2018-03-16 14:34 UTC] cmb@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.

Consider to use round(), number_format(), BCMath, or such.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 20:01:32 2024 UTC