Link to home
Start Free TrialLog in
Avatar of impressionexpress
impressionexpress

asked on

Creating csv using PHP giving errors on long numbers

I have a 9 digit number showing 3.14E+08 when exporting to CSV using fputcsv();
I tried to turn into a string which worked but quotes appeared which is also no good
any suggestions ?
$val = 123456789
$val = "'" . $val . "'"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Expanding the comment by Chris (which almost surely explains the problem).

Try reading the 9 digit datum into a variable, then printing the variable to see it's exact format.

Likely, as Chris mentioned, you'll see some sort of nomenclature used, which is different than pure numeric digits (scientific notation).

Once you print out + can see the exact notation, you can just use a sprintf(...) to confer back to pure digits.

This might be as simple as coding...

$val = import_your_datum(...)
$val = "'" . int($val) . "'"

Open in new window


You'll have to dig into the int() function to determine if int() or sprintf() is required to do your conversion.
Avatar of skullnobrains
skullnobrains

@chris good call. nevertheless i confirm the issue, at least with php5.5 which is what i have on this old laptop
not sure it is better in other versions

$ echo '<?php fputcsv(fopen("php://output","w"),array(23456789784565,"yadaa"));' | php
23456789784565,yadaa

$ echo '<?php fputcsv(fopen("php://output","w"),array(2345678978456545568465,"yadaa"));' | php
2.3456789784565E+21,yadaa

Open in new window


var_export, json encode and many functions behave in that same way.

this works fine

$ echo '<?php printf("%d",23456789784565455);' | php
23456789784565455

Open in new window


you need to write a custom func : something along those lines


function mkcsv($in){
  $ret='';
  foreach($in as $fld){
    if(is_float($fld))return false; // handle this your own way
    return (is_double($fld)?printf('%f',$fld):var_export($fld,true)).',';
  }
  $ret[strlen($ret-1)]="\n";
  return $ret;
}

the above works properly for doubles
i am quite unsure there even is a way to format a very large float properly without using external mathematical libs such as gmp or bcmath
larger numbers can probably be formatted with something along those lines

printf (%d%d , $in>>>64 , $in % pow(2,64))
Avatar of impressionexpress

ASKER

Chris Stanyon, you were right, I opted to save the file and open with notepad instead of opening the file with the suggested program being excel.

However, when doing this, Im seeing that the headers all have double quotes ?

This is no good, below is my header array. I also attached a screenshot of the result. User generated image
	// save the column headers
	$headers = array('BLOCK DESCRIPTOR', 
					 'RECORD DESCRIPTOR', 
					 'PROCESSING INDICATOR',
					 'TIME STAMP',
					 'CORRECTION INDICATOR',
					 'IDENTIFICATION NUMBER (MEMBER NUMBER)',
					 'CYCLE IDENTIFIER',
					 'CUSTOMER ACCOUNT NUMBER',
					 'PORTFOLIO TYPE',
					 'ACCOUNT TYPE',
					 'DATE OPENED',
					 'CREDIT LIMIT',
					 'HIGH CREDIT',
					 'TERMS DURATION',
					 'TERMS FREQUENCY',
					 'SCHEDULED PAYMENT AMOUNT',
					 'ACTUAL PAYMENT AMOUNT',
					 'ACCOUNT STATUS',
					 'PAYMENT RATING',
					 'PAYMENT HISTORY PROFILE',
					 'SPECIAL COMMENT',
					 'COMPLIANCE CONDITION CODE',
					 'CURRENT BALANCE',
					 'AMOUNT PAST DUE',
					 'ORIGINAL CHARGE OFF AMOUNT',
					 'DATE REPORTED',
					 'DATE OF FIRST DELINQUENCY',
					 'DATE CLOSED',
					 'DATE OF LAST PAYMENT',
					 'CONSUMER TRANSACTION TYPE',
					 'LAST NAME',
					 'FIRST NAME',
					 'MIDDLE NAME',
					 'GENERATION CODE',
					 'SIN',
					 'BIRTHDATE',
					 'TELEPHONE NUMBER',
					 'ASSOCIATION CODE ( WHOSE ACCOUNT)',
					 'CONSUMER INFORMATION INDICATOR',
					 'COUNTRY CODE',
					 'ADDRESS1',
					 'ADDRESS2',
					 'CITY', 
					 'PROVINCE',
					 'POSTAL CODE',
					 'ADDRESS INDICATOR',
					 'RESIDENCE CODE');

Open in new window

I found the solution to the double quotes, since Chris was first to give the right answer on the original question, I will accept his answer.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
good to know you got it working. nevertheless beware the trick using quotes will only work up to 2^64. maybe 2^32 on some systems.