We help IT Professionals succeed at work.

Creating csv using PHP giving errors on long numbers

impressionexpress
on
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

Comment
Watch Question

SILVER EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
I suspect this has more to do with how you're reading the CSV file, than how it's being generated.

Often, people open CSV files as a spreadsheet, using a program such as Excel, and by default, long numbers will be formatted using Scientific notation (3.14E+08). This can be caused by your column simply not being wide enough to show the full number (widen it and it may solve your problem)

First off, open your CSV file with a text editor, such as notepad. I'm guessing that you'll see the numbers written out correctly.
David FavorFractional CTO
BRONZE EXPERT
Distinguished Expert 2019

Commented:
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.
BRONZE EXPERT

Commented:
@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
BRONZE EXPERT

Commented:
larger numbers can probably be formatted with something along those lines

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

Author

Commented:
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

Author

Commented:
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.
SILVER EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Glad you got it sorted.

Something to be aware of. If you set the enclosure to NULL (so you don't get the quotes), make absolutely sure than none of your data has a comma in it, otherwise your CSV will break.
BRONZE EXPERT

Commented:
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.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.