1

Currently I have a number that is 19 digits long, when I try to print it out it gives me the scientific notation, which is exactly what I don't need since this is a specific ID rather than a number.

I've counteracted this by using number_format() however with large digits because of processing the number and the amount of processing I'm doing load time has gone up as well as the last 4 digits being incorrect. My environment is limited so loading in other modules may not be possible, what would be my best option?

And just for an example:

$cid = 8162315223029015401;
$cid1 = sprintf($cid);
$cid2 = number_format($cid, 0, '.', '');
echo gettype($cid);
echo $cid1;
echo $cid2;

>> double
>> 8.162315223029E+18
>> 8162315223029015552

Thanks in advance!

Thanks guys, issue is max length on x32 systems. Is there any way to maximize PHP_INT_MAX?

10
  • What's your expected format? Commented Mar 11, 2014 at 13:26
  • Expected output from echo should be "8162315223029015401", I am displaying this data on the page. Commented Mar 11, 2014 at 13:27
  • 1
    number_format() expects its first argument to be a floating point value, which means you're mangling your int. Commented Mar 11, 2014 at 13:28
  • 32-bit or 64-bit PHP? Commented Mar 11, 2014 at 13:31
  • more over better to use sprintf which takes care of floating point issues like showing exponential value !! Commented Mar 11, 2014 at 13:35

2 Answers 2

0

Use a string; $cid = "8162315223029015401";

Sign up to request clarification or add additional context in comments.

4 Comments

won't matter. number_format() expects a float. passing in an int or int-as-string will just "float-ize" the number and mangle the less significant digits
The data comes from an object and using settype($cid, "string") still yields a scientific notation, any ideas?
But it only needs to print it out, so we can leave out number_format? Otherwise I'm not understanding the question correctly.
There's a problem with PHP handling these big integers, to make it more complicated it depends if you are on 32 or 64 bit OS. See this answer for more information.
0

Use sprintf:

$cid = 8162315223029015401;
$cid1 = sprintf("%.8f", $cid);
$cid2 = number_format($cid, 0, '.', '');
echo gettype($cid) . PHP_EOL;
echo $cid1 . PHP_EOL;
echo $cid2 . PHP_EOL;

Output:

integer
8162315223029015552.00000000
8162315223029015552

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.