|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-08-20 08:50 UTC] stephane at it-asia dot com
Description:
------------
I have different value when I cast a double to int and when I cast to string before casting to int.
I understand 39.48 is difficult to store in base 2.
The problem is the cast algorythm is not the same if you cast a float to int or if you cast a float to string, This involves huges mistakes in accountancy software. Whatever the way you choose (float -> int or float -> string -> int ) , you should have the same result at the end.
Please define the right way to process data in that case.
I have the same problem with almost every machines, Windows or Debian based.
Thanks !
Test script:
---------------
$d = "39.48" * "100";
print("39.48 * 100 : ");
var_dump ($d);
$i = (int) $d;
print("<br />int: ");
var_dump ($i);
$s = (string) $d;
print("<br />string: ");
var_dump ($s);
$i = (int) $s;
print("<br />int: ");
var_dump ($i);
Expected result:
----------------
same value if you cast double => int and if you cast double => string => int
Actual result:
--------------
39.48 * 100 : double(3948)
int: int(3947)
string: string(4) "3948"
int: int(3948)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 27 02:00:02 2025 UTC |
var_dump() is giving you the real value based on your precision setting. If you add ini_set('precision',32); to the top of your test script, you will see: float(3947.9999999999995452526491135359) float(3948) int(3947) int(3948) And this is exactly the same as C and Python. And no, there won't be a money type in PHP. This is trivial to do in user space.