RobertPuerco RobertPuerco - 3 months ago 28
PHP Question

Float to bigint

Ok, so I have this float number :-

$floatval = '1.05143617E+18';


It's equivalent integer is :-

1051436170000000000


Using php, I'm trying to convert this float number to the required integer value.

Here is my try :-

$floatval = '1.05143617E+18';
var_dump(convert($floatval));

function convert($floatval)
{
$divided = explode('+', $floatval);
$first = floatval($divided[0]);
$second = intval($divided[1]);
$final = intval($first * pow(10, $second));
return $final;
}


Output :-

953738112


Whatever I try, the output is not coming as required. Where am I doing mistake?

Answer
$floatval = (float) '1.05143617E+18';
$intval = (int) $floatval;
var_dump($intval);

Output: int(1051436170000000000)

Comments