Kentot
Kentot

Reputation: 542

PHP format number from CSV file after upload

I just successfully uploaded and read through on a csv file using PHP Codigniter. What I want to do now is to format the numbers for example:

6.06861E+11

to

606861000000

before inserting it to the database. How should I do this in PHP? Thank you.

Upvotes: 1

Views: 898

Answers (2)

jhegeman2
jhegeman2

Reputation: 59

PHP understands scientific notation, you just need to let it know that you want it formatted as a number. You can accomplish this by subtracting zero from it like so.

<?php
$x = '6.06861E+11';
echo $x - 0;
?>  

Displays -> 606861000000

Upvotes: 1

ken lun
ken lun

Reputation: 22

Use the following function to convert from exponential to the right number format:

     function easy_number_format($number, $dec_point, $thousands_sep)
       {
       $number = rtrim(sprintf('%f', $number), "0");
       if (fmod($nummer, 1) != 0) {                      
       $array_int_dec = explode('.', $number);
       } else { 
       $array_int_dec= array(strlen($nummer), 0);
       }
       (strlen($array_int_dec[1]) < 2) ? ($decimals = 2) : ($decimals =                             strlen($array_int_dec[1]));
      return number_format($number, $decimals, $dec_point,                  $thousands_sep);
        } 

Upvotes: 0

Related Questions