开发者

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

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

开发者_StackOverflow中文版
$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???


This is a precision issue inherent to floating point numbers in PHP, and lots of other languages. This bug report discusses it a bit, in the context of casting as an int:

http://bugs.php.net/bug.php?id=33731

Try round($val * 100) instead.


The usual answer to this kind of question is to read What Every Computer Scientist Should Know About Floating-Point Arithmetic.


Why does intval(1990) equal 1989???

Because you're not taking intval(1990). You're taking intval($val * 100) where $val is a number close to, but slightly smaller than, 19.9.

Read The Floating-Point Guide to understand why this is so.

As for how to fix it: don't ever use floating-point values for money. In PHP, you should use BCMath instead.


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


$val is a floating point number - the result of "19.9" * 100. Floating point numbers are not 100% accurate in any language (this is by design). If you need 100% decimal accuracy for dollar values, you should use integers and perform all calculations using cents (E.g., "$19.90" should be 1990).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜