John Davidson

csv - PHP fputcsv showing scientific notation instead of long intergers

0 comments
Message:


When creating file in csv using fputcsv then long integers show in scientific notation.


Example -
0133211000000140 is showing 1.33211E+14


    fputcsv($file, array('Id','DESTINATION_ACCOUNT','SOURCE_ACCOUNT'));
if(!empty($data)){
foreach($data as $key => $row){
fputcsv($file,[$row['ID']
$row['DESTINATION_ACCOUNT'], -- scientific notation instead of long integer
$row['SOURCE_ACCOUNT'] -- scientific notation instead of long integer
]
);
}
}


My Answer:
> One way to prevent long integers from being displayed in scientific notation when using fputcsv in PHP is to cast the integer values as strings before writing them to the CSV file. This can be done by enclosing the integer values in double quotes.

Here's an example of how you can modify your code to achieve this:

php
fputcsv($file, array('Id','DESTINATION_ACCOUNT','SOURCE_ACCOUNT'));

if(!empty($data)){
foreach($data as $key => $row){
$id = '"' . $row['Id'] . '"';
$destination_account = '"' . $row['DESTINATION_ACCOUNT'] . '"';
$source_account = '"' . $row['SOURCE_ACCOUNT'] . '"';

fputcsv($file, array($id, $destination_account, $source_account));
}
}


By enclosing the integer values in double quotes, you are telling PHP to treat them as strings, which will prevent them from being displayed in scientific notation in the CSV file.

Rate this post

5 of 5 based on 4458 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us