14

I am working with huge numbers for website purposes and I need long calculation. When I echo a long number I don't get the correct output.

Example

// A random number $x = 100000000000000000000000000; $x = number_format($x); echo "The number is: $x <br>"; // Result: 100,000,000,000,000,004,764,729,344 // I'm not getting the value assigned to $x 
4
  • 3
    Its because its way far greater than the PHP_INT_MAX value. The exact numbers are 2147483647 (32-bit) and 9223372036854775807 (64-bit). php.net/manual/en/language.types.integer.php Commented Feb 3, 2016 at 6:31
  • so what i should i use? if its not support or something Commented Feb 3, 2016 at 6:32
  • This might help you stackoverflow.com/questions/19023840/… Commented Feb 3, 2016 at 6:32
  • 2
    yes i am aware of int bigint and of course decimals but is there any solution ? Commented Feb 3, 2016 at 6:33

5 Answers 5

9

Your number is actually too big for php standard integers. php uses 64 bit integers which can hold values within range -9223372036854775808 (PHP_INT_MIN) to +9223372036854775807 (PHP_INT_MAX).

Your number is about 87 bits long which simply is too much.

If you really need such big numbers you should use the php BC math types, explained in the manual: http://php.net/manual/en/ref.bc.php

If you just want to format a string formed like a huge number then use something like this:

function number_format_string($number) { return strrev(implode(',', str_split(strrev($number), 3))); } $x = '100000000000000000000000000'; $x = number_format_string($x); echo "The number is: $x\n"; // Output: The number is: 100,000,000,000,000,000,000,000,000 

Edit: Added strrev() to function because the string needs to be reversed before splitting it up (thanks to @ceeee for the hint). This ensures that the delimiter is placed at right position when length of input is not divisible by 3. Generated string needs to be reversed afterwards again.

Working example can be found at http://sandbox.onlinephpfunctions.com/code/c10fc9b9e2c65a27710fb6be3a0202ad492e3e9a

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

7 Comments

yes is there something i can do like converting it to decimal ?
Added example on how to format an integer number contained in a string.
thanks let me try with my more bigger nubmers once :)
that worked need to make my own function for more longer numbers but thanks alot it got me an idea :)
Its really a great example as you don't need to use BC match type of PHP +1
|
6

answer @maxhb has bug. if the input is '10000000000000000000000' the out put would be:

The number is: 100,000,000,000,000,000,000,00 

Which is incorrect. So try below code:

function number_format_string($number, $delimeter = ',') { return strrev(implode($delimeter, str_split(strrev($number), 3))); } $x = '10000000000000000000000'; $x = number_format_string($x); echo "The number is: $x\n"; // Output: The number is: 10,000,000,000,000,000,000,000 

4 Comments

My solutions does not output what you stated, it outputs what I wrote, see sandbox.onlinephpfunctions.com/code/…
@maxhb Hi Sir, Try 10000000000000000000000
thats working fine unless we give $x = 100000000000000000000000000;
or try $x = 1000. Output would be The number is: 100,0
2

The largest integer that can be represented in a 64bit PHP install, compared to your number:

 9,223,372,036,854,775,808 - largest possible signed 64bit integer 100000000000000000000000000 - your number 

since you're exceeding the maximum number size, you can't expect to get useful results without using something like gmp/bcmath.

Comments

0

PHP does not yet support formatting long numbers, even when you always keep them as strings in your code (to avoid issues with PHP’s int type):

php > echo number_format('100000000000000000000000000'); 100,000,000,000,000,004,764,729,344 php > echo number_format('3.14159265358979323846', 20); 3.14159265358979311600 

The underlying ICU library supports formatting arbitrary precision decimal numbers, but PHP doesn’t use the relevant function yet – see request #76093.

Comments

-1

http://php.net/manual/en/function.number-format.php

That is the solution:

<?php # Output easy-to-read numbers # by james at bandit.co.nz function bd_nice_number($n) { // first strip any formatting; $n = (0+str_replace(",","",$n)); // is this a number? if(!is_numeric($n)) return false; // now filter it; if($n>1000000000000) return round(($n/1000000000000),1).' trillion'; else if($n>1000000000) return round(($n/1000000000),1).' billion'; else if($n>1000000) return round(($n/1000000),1).' million'; else if($n>1000) return round(($n/1000),1).' thousand'; return number_format($n); } ?> 

1 Comment

No i am not looking for converting my long number to short with billions or trillions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.