Robert
Robert

Reputation: 10380

php converting float to int using type casting

So I am reading the php manual on its types and am playing around with them. I have:

var_dump((int)0.8 + 0.2)

why does this return 0.2?

I have no idea why.

Upvotes: 0

Views: 78

Answers (1)

Erlesand
Erlesand

Reputation: 1535

It first evaluates (int)0.8, which is 0, then it adds 0.2. You need to use,

var_dump((int)(0.8 + 0.2))

Upvotes: 6

Related Questions