How to convert scientific notation to full number string

Asked

Viewed 3,012 times

4

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

Convert: 1.3388383658903E+18 to: 1338838365890273563

I tried that, 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:

Take this example with serialize:

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

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

Example in the ideone

  • 1

    So it doesn’t work $notation = 1.3388383658903E+18;
printf('%.0F', $notation); ?

  • Tried with number instead of string?

  • I don’t understand why you used two sprintf(sprintf?

  • 1

    I didn’t understand was nothing, I took from an example... that didn’t work.

  • 2

    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?

4 answers

3


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).

0

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

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

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

Andexample using function

-1

$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);
  • If you multiply the value by 100000000 you are changing the value.

  • No, because it follows a mathematical principle :) ex: it will multiply by one and add the zeros. equal 10 x 10 = 100 (if you go to see the 0 from one comes to the other) the same thing repeats in the notation, can make the test :) I tested with fractions of cryptocurrencies and worked so posted here

  • The point is that the value $notation * 100000000 will be 1.3388383658903E+26, in order of magnitude 26, not 18 as is the original number.

  • well I tested it here and it was good, because when it puts the notation inside the number_format it already changes to the original numbering the part of multiplying is only for small values ex: 0.00000001 <- in cryptocurrency Internet the Site responds in notation,putting that way and multiplying is in the normal value

-7

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

Ex.:

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

This is because PHP is a weak typing language.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.