11

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???

6
  • intval('1990') gives me 1990. I'll try to check your case out. In the while, please try to give a more relevant title to your question.
    – zneak
    Mar 23, 2010 at 3:40
  • No, no. intval('1990') gives 1990, what I'm saying is that the example suggests indirectly that intval(1990) equals 1989 Mar 23, 2010 at 3:41
  • 1
    This issue has been discussed in about every other language known to man on this site...
    – animuson
    Mar 23, 2010 at 3:44
  • @animuson: BUT it is manifest differently in each language and can be elusive in some of them. Mar 23, 2010 at 3:45
  • 2
    I really do NOT recommend using floating point values for money. When handling money, use ONLY integer values in terms of cents. Otherwise you'll be effected by all sorts of issues that you'll never be aware of until it's too late. It could and likely will cause innocent people to have their money vanish on them (a fraction at a time) for no reason other than your choice of method for handling monetary values.
    – Geoff
    May 12, 2010 at 19:30

5 Answers 5

17

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.

3
  • Okay, round gives the expected answer. What implications does using round instead of intval have? Mar 23, 2010 at 3:45
  • as I understand it, casting the value to an int using intval() essentially chops the float value after the decimal instead of rounding it.
    – Funkatron
    Mar 23, 2010 at 3:48
  • Because round rounds the number to the closest integer and intval simply takes the integer part of the floating point number (1989 in this case) and ignores the rest of it.
    – animuson
    Mar 23, 2010 at 3:48
3

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

4
  • 1
    ...which is unfortunately no help at all to people asking this kind of basic questions. May 12, 2010 at 19:33
  • 1
    and not even any help to those of us who produce a lot of code, but aren't mathematicians.
    – wadesworld
    May 12, 2010 at 19:37
  • I saw in another answer that someone had written a simplified version of this document, but I don't remember to what question or whom it was.
    – Powerlord
    May 12, 2010 at 19:51
  • That was me, see my response for the link :) May 12, 2010 at 20:27
2

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.

1

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
0

$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).

5
  • So then... what are you saying? Mar 23, 2010 at 3:44
  • Using integers is not really a good solution at all. Have to deal with amounts larger than 20 million? Oops. Different currencies, not all of which have 100 sub-units? No can do. May 12, 2010 at 19:44
  • @Michael - PHP doesn't have 64-bit integers? I think different currencies are a non-issue, since you can't compare them to each other directly anyway. May 12, 2010 at 20:11
  • @Joel: PHPs integer type is platform-dependant. Could be 64bits, could be 32. You don't get to choose. And all it takes to compare currencies is an exchange rate. But even if you don't compare and only store them, you have to do so with a varying number of fractional digits. May 12, 2010 at 20:25
  • Sure, but if you're converting different currencies into integers, you just need to know the number of fractional digits for that currency, so you multiply by the right value. The fact that they might not have the same number of fractions is irrelevant, as long as you don't lose track of which currency this particular integer represents. May 12, 2010 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.