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

PHP

Avatar of undefined
Last Comment
skullnobrains

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David Favor

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

larger numbers can probably be formatted with something along those lines

printf (%d%d , $in>>>64 , $in % pow(2,64))
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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. screenshot
	// 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

impressionexpress

ASKER
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
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
skullnobrains

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.