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

I worked with PHP code, which would be the expected result:

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), showing the actual number, and then
1.0E + 14
1.0E + 16
1.0E + 18


I found some solutions there! they said using sprintf or by 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
10001
1000001
100000001
10000000001
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 does not show actual large numbers. ok, the two ways looked good, but that doesn't match what I want. can anyone resolve this?

+6
source share
3 answers

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

+6
source

I suggest you take a look at the BC Math extension in php.

+2
source

You must definitely use BCMath.

If you want to know why you cannot do this with ordinary numbers, this is because they are floating point numbers, and they use a fixed amount of memory space (more precisely, 64 bits), so their accuracy is physically limited. Otherwise, the line is not limited, because it would be stupid if the length of the text was limited. This is why BcMath and other libraries use a string for arithmetic calculation.

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

A good day

+2
source

Source: https://habr.com/ru/post/952081/


All Articles