12

I want to be able to convert big ints into their full string derivatives.

For example.

$bigint = 9999999999999999999;
$bigint_string = (string) $bigint;
var_dump($bigint_string);

outputs

string(7) "1.0e+19"

but i need

string(19) "9999999999999999999"

Please don't tell me that I should just originally set the $bigint value as a string. That is not an option. I really am stuck and don't know if it is even possible?

buggedcom
  • 1,537
  • 2
  • 18
  • 34

4 Answers4

13

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"
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 3
    Thanks hakre for the in-depth response. I guess I have got plenty more to learn. – buggedcom Aug 22 '11 at 17:56
  • gmp_init() needs to start with a string or an int -- not a float. Which means, if you've already got it in a string or an int, you don't need gmp. – Alex Howansky Aug 22 '11 at 17:59
  • @buggedcom: You're welcome. There is always something new to learn ;) – hakre Aug 22 '11 at 18:06
  • this doesn't work if you pass it like this: $bigint = gmp_init(9999999999999999999); – Phill Pafford Nov 04 '11 at 15:13
  • @PhillPafford: Pass the number as integer or string, not float. – hakre Nov 05 '11 at 06:25
  • @Alex Howansky: Well, you can't do integer operations with strings in PHP if they are above PHP's integer limit, but gmp can. – hakre Nov 05 '11 at 06:28
  • @hakre: Yes, but there's a catch-22 here. In this particular question, the OP isn't doing operations, he simply wants a string representation of a big int. He would have to pass a string to gmp_init() to get it. Which means he'd have to initialize the function with the very string that he was looking to get back. I.e., your example requires that he has the value in string format in the first place, and if that were the case, then he'd already have what he was looking for and he wouldn't need gmp. (Granted, it's not clear where the initial value is coming from, so... who knows... :) – Alex Howansky Nov 05 '11 at 16:11
  • Well, the source must be a string. Isn't all PHP input string? – hakre Nov 05 '11 at 16:18
4

Use strval().

echo strval($bigint); // Will output "99999999999999999"

EDIT: After a slightly better research (> 5000ms), I see that it is impossible to echo numbers to that precision, because PHP cannot save an integer of that size (depends on OS bit system, 32/64). Seems like it's not possible to do it without losing precision.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Just store the value as string and display...!

   $bigint = '9999999999999999999';

    var_dump($bigint);

Output:

string(19) "9999999999999999999"
ShivarajRH
  • 902
  • 12
  • 24
0

Use printf(), it's made for that:

echo printf("%d", $bigint);
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194