Convert a Big Integer to a Full String in PHP

PHP: How to convert bigint from int to string?

You actually should ensure that you type what you mean:

$bigint = 9999999999999999999;

Is not a PHP integer but float:

float(1.0E+19)

If you would have done

$bigint = (int) 9999999999999999999;

You would have set an integer in fact, but it would not be the number you might have expected:

int(-8446744073709551616)

It's no problem at all to turn that into string as you might have guessed. So take care when you write numbers into code that you actually write what you mean.

See this line again:

$bigint = 9999999999999999999;

try to understand what you have actually written. It's not an integer at all because PHP will turn it into a float. See Example #4 Integer overflow on a 64-bit system in the integer manual page.

If you need higher precision, checkout GNU Multiple Precision, it might have what you're looking for. However, this won't change how to write numbers in PHP:

$bigint = gmp_init("9999999999999999999");
$bigint_string = gmp_strval($bigint);
var_dump($bigint, $bigint_string);

Output:

resource(4) of type (GMP integer)
string(19) "9999999999999999999"

Convert a big integer to a full string in PHP

This is not stored as an integer by PHP, but a float, this is why you end up with 1.0E+25 instead of 10000000000000000000000000.

It's sadly not possible to use that as an integer value in PHP, as PHP cannot save an integer of that size. If this comes from database then it will be a string and you can do with it whatever you want. If you store it elsewhere then store it as a string.

Your alternative is to store it as a float and take that into account at all times, though that requires additional conversions and handling in places.

It's also been suggested to use GNU Multiple Precision, but that's not enabled in PHP by default.

$int=gmp_init("10000000000000000000000000");
$string=gmp_strval($int);
echo $string;

Convert big integer or float to string exactly as they are

Seems like you're using json_decode. It will convert numbers to integers where possible, otherwise float:

// 32bit integers
var_dump(json_decode("2147483647")); // int(2147483647)
var_dump(json_decode("2147483648")); // float(2147483648)

// 64bit integers
var_dump(json_decode("9223372036854775807")); // int(9223372036854775807)
var_dump(json_decode("9223372036854775808")); // float(9.2233720368548E+18)

You can use JSON_BIGINT_AS_STRING flag so that json_decode decodes large integers as their original string value:

// 32bit integers
var_dump(json_decode("2147483647", false, 512, JSON_BIGINT_AS_STRING)); // int(2147483647)
var_dump(json_decode("2147483648", false, 512, JSON_BIGINT_AS_STRING)); // string(10) "2147483648"
// 64bit integers
var_dump(json_decode("9223372036854775807", false, 512, JSON_BIGINT_AS_STRING)); // int(9223372036854775807)
var_dump(json_decode("9223372036854775808", false, 512, JSON_BIGINT_AS_STRING)); // string(19) "9223372036854775808"

Note that the number-as-a-string is not really useful for arithmetic e.g. you cannot add or multiply from it without it (auto) converting to float.

PHP -- Convert string to bigint

MySQL isn't going to know the difference. Just feed the string into your query as though it is a number without first type casting it in PHP.

For example:

$SQL = "SELECT * FROM table WHERE id = $test";

PHP: Converting big integer from string to int with overflow

here is the workaround.

<?php
$unsignedString = "3000000000";
echo "unsigned string: ".$unsignedString."<br />";
$signedInt = intval(doubleval($unsignedString));
echo "signed int: ".$signedInt."<br />";
?>

PHP: Converting a 64bit integer to string

You're losing the precision on the assignment, not on the string conversion. If this variable's value is actually hardcoded, and you can't change that, there's nothing you can do.

A line like:

$i = 76561197961384956;

will always lose precision. If you need to keep the whole thing, store it into a string, you can't store it as an int like that and keep all the digits.



Related Topics



Leave a reply



Submit