0

I have data from csv, the contents of the column id look like this 6.61884E+17 6.61881E+17 6.61878E+17

This code not work, the results of this code remains the same :(

$a = '6.61884E+17';
$b = (string) floatval($a);

How to convert 6.61884E+17 to string ?

Update :

I try number_format(6.61884E+17,0,'','') and get 661884000000000000 as result. . my expected result is 661883847445868544

Anatasya
  • 1
  • 1
  • Use sprint() or similar.... `$a = '6.61884E+17'; $b = sprintf('%f', floatval($a)); echo $b;` – Mark Baker Nov 06 '15 at 09:48
  • This should solve your problem: http://stackoverflow.com/a/4964120/1610183 – Andrius Nov 06 '15 at 09:49
  • Technically that number is right. PHP converts large integers to floats with an exponent (losing precision). You 'could' use MCBath to expand it, but all you will get is a very large number with a lot of zeros on the end. – Flosculus Nov 06 '15 at 09:52

1 Answers1

0

You want the number 661883847445868544 to be extracted from 6.61884E+17.
This isn't possible for you because of "integer overflow".

PHP handles this for you by converting it into a float (it's stupid because you can't perform basic arithmetic on them).

Now, if you run echo strval(661883847445868544); and get the same value back, you are running PHP 64 bit, and can handle that number. However, what is clear is that the program that created the CSV is 32 bit, and the largest integer values are:

2,147,483,647 signed
4,294,967,295 unsigned

After the value 6.61884E+17 is dumped, precision after the 6th digit is lost, and there is nothing you can do to bring it back. Sorry.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • I understand now, thanks for your explanation ;) – Anatasya Nov 06 '15 at 10:08
  • @Anatasya Having said that. if you are in control of the CSV's generation, and the program supports arbitrary precision (PHP has bcmath), then you can dump the complete value. But arbitrary precision is infectious, once you start depending on it, you will never be able to stop using it, so its best to work around the problem. – Flosculus Nov 06 '15 at 10:16