1

I am trying to understand converting from a float to an int in PHP ,and came across the following code:

echo (int) ( (0.1+0.7) * 10 ); // echoes 7!

My question is why does it echo 7 instead of 8? What is the logic behind it?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102

3 Answers3

1

Here:

$var = (int) $othervar;

I would guess:

$var = int($othervar);

would work too. BUT IT DOES NOT!

gettype() will tell you the type, get_class() will tell you the class of an object, both return strings, get_class() returns the current class if no argument or null is provided, if you provide a non-object to get_class() it's some sort of error.

These functions are an example of how bad php, gettype() and get_class(), fun fact, because you have to play "guess which has an underscore" you also have is_a() (function, expects $var and "typename") and instanceof (operator).

Instead it is:

$var = intval($othervar);

and there's floatval, the whole lot have val after them! More PHP sillyness.

Note that:

$var = (string) $othervar;

invokes $othervar's __toString() method, but there isn't a __toInt() method, just strings.

Hope this helps.

Addendum:

Integers are always truncated, hence 7.

Other addendum:

If you want more controlled conversion you can use round() which works as one would expect, floor() (rounds down, so 7 less x less 8 floors to 7, -3 less x less-2 floors to -3) and ceil() which rounds up (7 less x less 8 ceils to 8, -3 less x less -2 ceils to -2)

This question is an almost exact copy of:

Why would on earth PHP convert the result of (int) ((0.1+0.7)*10) to 7, not 8?

Even the example!

Community
  • 1
  • 1
Alec Teal
  • 5,770
  • 3
  • 23
  • 50
  • The only relevant part of this answer is the `addendums`. – John V. Aug 24 '13 at 02:10
  • BUT the entire answer was useful, I once thought PHP was the best thing since unsliced bread became obsolete, it covers simple conversion, lack of cast functions, the weird form of intval, the line "Integers are always truncated" and then the three rounding functions, it's a complete, if it were 40 pages long I'd prune it of course, but it isn't what I'd call "a long answer", I get that would be subjective but this should fall on the "not long" side of anyone's line surely. – Alec Teal Aug 24 '13 at 02:13
  • Still, the part that actually answers the question shouldn't be tagged on the end of your rant about php being weird. – John V. Aug 24 '13 at 02:14
  • Perhaps, I misread the question initially, I read the title and the first sentence and started typing my answer, I've opted to insert an addendum rather than just put it in my answer because it was added post-submission, would a re-structure of the answer be worth it do you think? Given how many duplicate questions there are out there is it even worth this comment section? Anyway, yeah I went for the lazy way but gave a complete answer for anyone searching, should they read it. If you want me to edit I shall, I am newer than you here, so I would deffer to what you suggest. – Alec Teal Aug 24 '13 at 02:20
  • In my opinion if you write an answer, you should always work to make it the best possible, and putting the part that directly answers their question first helps, you can definitely leave the rest of it after that though. – John V. Aug 24 '13 at 02:23
0

Because the actual value stored in memory is something like 7.999999999999999999999999 which is caused by rounding error.

Žan Kusterle
  • 572
  • 1
  • 10
  • 28
0

you can also use intval()

$float = 7.99999999999999999999999999999999;
$int = intval($float);

echo $int;
DevZer0
  • 13,433
  • 7
  • 27
  • 51