On the numerical accuracy:
The question asks something that is not possible, convert 1.3388383658903E+18
for 1338838365890273563
.
The notation 1.3388383658903E+18
is only accurate 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 the quotes in the value. Note that even if your code is written the literal value:
$valor = 1338838365890273563;
internally PHP will only store 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/en/language.types.integer.php
About the exhibition of scientific notation:
That’s all it takes:
echo sprintf( '%f', 1.3388383658903E+18 );
Or that, of course:
$numero = '1.3388383658903E+18'; // o ideal mesmo é sem aspas
echo sprintf( '%f', $numero );
If you prefer without the decimals:
echo sprintf( '%.0f', 1.3388383658903E+18 );
See working on IDEONE.
If it’s just for show on screen:
Then you don’t even need the echo, just use printf
in place of sprintf
:
printf( '%.0f', 1.3388383658903E+18 );
I kept the sprintf
in the original example, because it is usually what will be used if you store the value in a string, or concatenate with something else.
Remarks:
We can’t use %d
, because the capacity of integers is burst;
the use of .0
before the f
serves to say that we want zero decimal places;
there is no need to quote the value because the format nE+n
is already understood as number by the language of course. But see in IDEONE that the problem is not this, because PHP does the cast anyway.
makes no difference in our case, but beware, because %f
and %F
are different things. Both are float, but one of them is locale-aware
(which changes the sign of decimals according to the region).
So it doesn’t work
$notation = 1.3388383658903E+18;
printf('%.0F', $notation);
?– rray
Tried with number instead of string?
– Bacco
I don’t understand why you used two
sprintf(sprintf
?– Guilherme Nascimento
I didn’t understand was nothing, I took from an example... that didn’t work.
– Ivan Ferrer
But then that code has no meaning
sprintf(sprintf('%%.%df', 0), '1.3388383658903E+18');
, at least for me. It makes no sense to do a sprintf of 0 and then use the result for another sprintf, it would be better to have read the documentation minimum, not agree?– Guilherme Nascimento