Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `intval(19.9 * 100)` equal `1989`?

Tags:

php

Boy, this one is really weird. I expect the following code to print 1990, but it prints 1989!

$val = '$19.9';

$val = preg_replace('/[^\d.]/','',$val);
$val = intval($val * 100);

echo $val;

Why on earth is this happening?

Edit: and this code:

$val = '$19.9';
$val = preg_replace('/[^\d.]/','',$val);
echo $val . "<br>";
$val = $val * 100;
echo $val . "<br>";
$val = intval($val);
echo $val;

Prints:

19.9
1990
1989

Why does intval(1990) equal 1989???

like image 990
Nathan Osman Avatar asked Mar 23 '10 03:03

Nathan Osman


1 Answers

i was facing similar issue with my code but got solution php.net

need to convert variable to string for intval operation e.g:

intval( 9.62 * 100 )  //gives 961
intval( strval( 9.62 * 100 ) )  //gives 962
like image 194
kshitij Avatar answered Sep 28 '22 05:09

kshitij