Float number to integer

I have this number
float(1.3098140391E+17)

number_format does not output an exact one…
How can I have the exact integer?

try using php’s floor() or ceil() to round a number down/up or casting the float as an int using $int = (int) $float.

But you might be exceeding the largest value allowed for an integer.

The reason you’re getting scientific notation is because the number is so huge. When I tried running intval on it, I got -1,807,954,560. Is there some reason you need the normal integer?

At least to a string… do not need an integer.

Value comes from an API query and need the exact number for future queries to the API server.

In terms of converting it back to the original integer, I think you’re out of luck. I was going to suggest that you could parse it, remove the decimal place, and calculate how many zeroes (“zeros”?) you would need to tack on to the end of it. That would give you an integer (as a string) that was approximately the same as the scientific notation (in the example you gave, the end result you’d be shooting for would be “130981403910000000”). But it’s pretty unlikely that you would be able to send that approximation back to the API and still have everything work.

Did you try sending the scientific notation, and it didn’t like it?

When you first received the integer, what was the format? JSON, XML, text?

It seems unlikely that you both received a number in E notation and it won’t accept a number in E notation. So (if I’m right about this) the good news is that there should still be a pretty simple workaround.

http://us3.php.net/gmp

It is json converting the large number to float after json_decode() function.
Just figured few minutes ago after posting the first post.

API is only providing query results in json, no XML :frowning:

Any idea to solve it?

Many thanks!

There’s probably an easier way, but you could put this code into a function and tweak it to suit.

This code outputs:


1.3098140391E+17
130981403910000000

<?php

$float = 1.3098140391E+17 . '';

$floatParts = explode('E', $float);

$strNum = $floatParts[0];
$num = (float) $floatParts[0];
$pow = intval($floatParts[1]);

$num =  $num * pow(10 ,strlen($strNum)-2);

echo $float.'<br />';

echo str_pad($num, $pow+1, '0');

?>
$string = '[{"id":130988929901015042,"b":2,"c":3,"d":4,"e":5}]';
var_dump(json_decode($string));

array(1) { [0]=> object(stdClass)#1 (5) { [“id”]=> float(1.3098892990102E+17) [“b”]=> int(2) [“c”]=> int(3) [“d”]=> int(4) [“e”]=> int(5) } }

Hi webdev1958,

I already figured in PHP manual the reverse function in not 100% correct.
Main problem is the API json query when it is decoded, large numbers become float.
Please, see my reply above

but that is not what you said in your original post.

You seem to be drip feeding information as you go along. I’m not going to spend more time on this in case there are more “surprises”.

Good luck :slight_smile:

Sorry! Usually I never do that but I am rushing this work and this is blocking me to go ahead…

Basically, this is what is happening

$string = '[{"id":130988929901015042,"b":2,"c":3,"d":4,"e":5}]';
$output = json_decode($string); 

echo $output->id; //float(1.3098140391E+17)

I would need this
echo $output->id; //130988929901015042

Again, I am very sorry…

It’s not the prettiest method, but I think it’s the simplest: Construct a regular expression and just pull the id from the string, before you convert it:


$regex = '~"id":(\\d+)~';
$matches = array();
preg_match($regex, $string, $matches);
$id = $matches[1];

ouch

There is some light at the end of the tunnel. It might be of much use to you right now but PHP 5.4 will make available an option to decode big integers as strings, avoiding the problems you’re having in this thread.


$decoded = json_decode('{"bigint":12345678901234567890}', true, 512, JSON_BIGINT_AS_STRING);
var_dump($decoded['bigint']);
// string(20) "12345678901234567890"

The easiest way I can think of is using sprintf’s %F specifier:

F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).

$num = 1.3098140391E+17;
$formatted = sprintf('%F', $num); // 130981403910000000.000000

// strip the fractional part
$formatted = preg_replace('/\\.[0-9]+$/', '', $formatted);

echo $formatted; // 130981403910000000

This will also work if $num is an integer, it doesn’t have to be a float. It can also be a string containing a numeric value (float or integer).

As a one-liner:

echo preg_replace('/\\.[0-9]+$/', '', sprintf('%F', 1.3098140391E+17));