Čamo
Čamo

Reputation: 4170

Php cast to int returns double

Can somebody tell me please why this code returns double in php?

$personal_id = ((int) $person['born_number'] * 2);

Gettype($personal_id) returns double like this 4294967294.0

How can I solve this? Thanks.

Upvotes: 1

Views: 149

Answers (4)

I. Koroteev
I. Koroteev

Reputation: 41

Why double? Because variable doens't fit into integer size.

You may use gmp for emulating big integer. http://php.net/manual/en/intro.gmp.php

But i think it's overengineering. What if use string instead of integer?

$personalId = sprintf("%.0f", ($a*2));

Upvotes: 1

Naveed Ramzan
Naveed Ramzan

Reputation: 3593

$personal_id = (int)($born_number * 2);

Upvotes: 0

Treast
Treast

Reputation: 1105

Maybe you should try intval() (cf. official documentation)

$personal_id = intval($person['born_number'] * 2);

Upvotes: 0

Dennisvdh
Dennisvdh

Reputation: 120

try this;

$personal_id = floor($person['born_number'] * 2);

Upvotes: 0

Related Questions