Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(PHP) How to avoid scientific notation and show the actual large numbers?

I have been working with the PHP code that the expected result will become :

1
101
10001
1000001
100000001
10000000001
1000000000001
100000000000001
10000000000000001
1000000000000000001


finally the output was :

1
101
10001
1000001
100000001
10000000001
1000000000001     // (1 billion) to (100 billion - 1) still showing the actual number, and then
1.0E+14
1.0E+16
1.0E+18


I've found some solutions there! they said by using sprintf or trying format_number. I tried both.

using sprintf and the result :

$format_x = sprintf("%.0f ",$x);
echo $format_x.$br;

1
101
10001
1000001
100000001
10000000001
1000000000001
100000000000001
10000000000000000
1000000000000000000


using format_number and the result :

echo number_format($x, 0).$br;

1
101
10,001
1,000,001
100,000,001
10,000,000,001
1,000,000,000,001
100,000,000,000,001
10,000,000,000,000,000
1,000,000,000,000,000,000


but it still doesn't show the actual large numbers. well, the two ways was looks good but it doesn't fit with what i want. does anyone can resolve this?

like image 960
Herry Kusmadi Avatar asked Aug 20 '13 06:08

Herry Kusmadi


People also ask

How do you express very large numbers in scientific notation?

Scientific notation is a way of writing very large or very small numbers. A number is written in scientific notation when a number between 1 and 10 is multiplied by a power of 10. For example, 650,000,000 can be written in scientific notation as 6.5 ✕ 10^8.

How do you change scientific notation to normal form?

Steps for Converting Scientific Notation to Standard FormStep 1: Identify the exponent in the power of 10. Step 2: Move the decimal that many places to the right if the exponent is positive and to the left if the exponent is negative. Step 3: Fill in any empty spaces with zeros.

How do you exit scientific notation?

Converting Numbers from Scientific Notation. Decide if you will be moving the decimal point to the left or to the right. If the exponent on the "x 10" part of the number is positive, then you will be moving the decimal places to the right; if the exponent is negative, you will be moving the decimal places to the left.

What is the term given to the way of displaying large numbers by shortening them and multiplying by a power of 10?

Scientific notation is simply a way of writing numbers. It is especially useful in expressing very large or very small numbers because it is shorter and more efficient and it shows magnitude very easily. Every real number can be written as a product of two parts: a decimal part times an integer power of ten.


2 Answers

You can use standard bc_math library to handle operations with large numbers. Please, note, that in this case your numbers will be represented as strings, but library provides specific methods for operations under such strings.

like image 109
Alma Do Avatar answered Sep 16 '22 15:09

Alma Do


You should definitely use BCMath.

If you want to know why you can't do it with regular numbers, it's because they are floating point numbers and they are using a fixed amount of space in the memory(64 bits to be exact) so their precision is physically limited. Otherwise a string is not limited because that would be stupid if the length of a text was limited. That's why BcMath and other libraries uses string for arithmetical computation.

If you want to learn more about the format used by PHP(and almost every language) to store big numbers you can go there : http://en.wikipedia.org/wiki/Double-precision_floating-point_format

Have a good day

like image 23
DARK_DUCK Avatar answered Sep 16 '22 15:09

DARK_DUCK