Bug #76105 [Opn->Nab]: String to Number conversions

From: Date: Fri, 16 Mar 2018 14:34:16 +0000
Subject: Bug #76105 [Opn->Nab]: String to Number conversions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214382@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76105&edit=1

 ID:                 76105
 Updated by:         cmb@php.net
 Reported by:        mail at fsax dot ch
 Summary:            String to Number conversions
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   all
 PHP Version:        Irrelevant
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2018-03-16 13:04:27] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=76105&edit=1


Thread (2 messages)

« previous php.bugs (#214382) next »