note 2316 deleted from printf by ronabop

From: Date: Fri, 25 Aug 2000 08:12:00 +0000
Subject: note 2316 deleted from printf by ronabop
Groups: php.notes 
Request: Send a blank email to php-notes+get-3121@lists.php.net to get a copy of this message
<B>warning:</B> the following command may not return what you expect: <PRE> printf ("(9.95 * 100) = %d <BR>n", (9.95 * 100)); </PRE> i am using php 3.0.11 and this command will print a result of 994. This also happens if you try to do an intval() first, so this looks like it is something to be wary of if you use float values at all and convert them to integers at any point. -- The one way that i have found around this is to add 0.5 before doing an intval() which seems to counteract whatever is happening under-the-hood to cause this problem, for example: <PRE> printf ("(9.95 * 100) = %d <BR>n", intval((9.95 * 100) + 0.5)); </PRE> this seems to work fine, but it is kind of alarming that it does not work otherwise -- which leads me to be skeptical of even this fix working every time because i am not sure why this would be necessary.

« previous php.notes (#3121) next »