How to convert scientific notation to string with full number

What php function should I use to do this type of conversion?

Convert: 1.3388383658903 and+18 to: 1338838365890273563

Tried this, but it didn't work:

echo sprintf(sprintf('%%.%df', 0), '1.3388383658903E+18');

And that too:

echo rtrim(sprintf('%.0F', $value), '0');

Edited so far to better explain the problem:

See this example with serialize:

$a = array('valor' => 1338838365890273563);
$serializado = serialize($a);

How to fix this when doing unserialize($serializado)?

Example in ideone

Author: Ivan Ferrer, 2016-10-04

4 answers

About numerical accuracy:

The question asks for something that is not possible, convert 1.3388383658903E+18 to 1338838365890273563.

The notation 1.3388383658903E+18 only has precision for the houses that are actually readable in the string.

If you need the original value, convert to string before of serialize:

$a = array('valor' => '1338838365890273563' );
$serializado = serialize($a);

See quotation marks in the value. Note that even if in your code the literal value is written:

$valor = 1338838365890273563;

Internally PHP will save only what fits in the float. Thus, it is essential that you treat the data as string throughout the "life" of the script.

If you need more numerical capacity, you can use bc_math and GMP, but in your case probably strings are a better solution. More details in the PHP manual:

Http://php.net/manual/pt_BR/language.types.integer.php


about scientific notation display:

Just that:

echo sprintf( '%f', 1.3388383658903E+18 );

Or so, clear:

$numero = '1.3388383658903E+18'; // o ideal mesmo é sem aspas
echo sprintf( '%f', $numero );

If you prefer without decimals:

echo sprintf( '%.0f', 1.3388383658903E+18 );

See working on IDEONE.

If it is only to show on screen:

Then you don't even need echo, just use printf instead of sprintf:

printf( '%.0f', 1.3388383658903E+18 );

I kept sprintf in the original example, because it is usually what will be used if it is to save the value in a string, or concatenate with something else.


remarks:

  • We cannot use %d because the capacity of integers is burst;

  • Using .0 before f serves to say we want zero decimal places;

  • Has no need for quotation marks in the value, since the format nE+n is already understood as a number by the language naturally. But see in IDEONE that the problem is not that, since PHP does the cast anyway.

  • Makes no difference in our case, but beware, for %f and %F are different things. Both are float, but one of them is locale-aware (which changes the sign of the decimals according to the region).

 3
Author: Bacco, 2020-06-11 14:45:34

Just for the record, there is a way to solve this serialize, which is passing the number to string, that no one thought:

$a = array('valor' => (string) 1338838365890273563);
$serializado = serialize($a);

echo $serializado;
echo "\n-------------------------\n";
var_dump(unserialize($serializado));

E xample using function

 0
Author: Ivan Ferrer, 2019-07-30 11:46:14
$notation = 1.3388383658903E+18;


//Com decimais(8) só alterar conforme for necessario.

echo "Decimal: " . number_format($notation ,8);

//Sem decimais(bem simples apenas adicione 1 e depois adicione a quantidade de zeros que possui na versão decimal)

echo "Normal: " . number_format($notation * 100000000);
 -1
Author: Dev Mr. PH, 2019-07-29 21:36:11

You add the scientific notation plus one (1) it will return the number in double/float.

EX.:

$notation = "1.3388383658903E+18";
echo $notation + 1;

This is because PHP is a weak-typed language.

 -7
Author: Vinicius Guedes, 2016-10-05 19:46:11