note 86590 added to function.intval
| From: | BenL at osu1 dot php dot net | Date: | Sat, 25 Oct 2008 00:37:25 +0000 |
| Subject: | note 86590 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145730@lists.php.net to get a copy of this message | ||
I was browsing around the site, and I seen some people commented on my comment down the way a while
back...
re: Disturbing issue with intval()
I was of the opinion then as I'm of the opinion now that if you are going to have a function
like intVal in a language in conjunction with a standard type casting syntax such as (int)$var, then
it should not be vulnerable to floating point issues.
It's obvious that it would be very easy to do so, as when you do: echo 19.99 * 100; or
intVal((string)(19.99 * 100));
it gives you 1999.
However if you do intVal(19.99 * 100), then echo it out, it outputs 1998...
This shows that type casting the float val of 19.99 * 100 to a string gives you a happy 19.99,
instead of outputting 19.989999...etc
I would expect this behavior of (int)(19.99 * 100)...
But not of the function intVal, which was my point of the comment in the first place. Once I
noticed that behavior a long time ago I started using round, on such values and only use
intVal/typecasting to int from string variables such as $_REQUEST variables that I expect to be in
integer format.
I still wish intVal handled all that for you though.
----
Server IP: 64.71.164.2
Probable Submitter: 68.15.184.251
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- https://master.php.net/note/edit/86590
Del: integrated -- https://master.php.net/note/delete/86590/integrated
Del: useless -- https://master.php.net/note/delete/86590/useless
Del: bad code -- https://master.php.net/note/delete/86590/bad+code
Del: spam -- https://master.php.net/note/delete/86590/spam
Del: non-english -- https://master.php.net/note/delete/86590/non-english
Del: in docs -- https://master.php.net/note/delete/86590/in+docs
Del: other reasons-- https://master.php.net/note/delete/86590
Reject -- https://master.php.net/note/reject/86590
Search -- https://master.php.net/manage/user-notes.php