How to keep trailing zeros in csv using php fputcsv?

265 Views Asked by At

I am using fputcsv function to write data in a file.But it is removing the trailing zeros from the output. Here is the code I am using

foreach($invoice_hdr_array as $csv_response) 
{
    fputcsv($fh1, $csv_response, ',', '"');  
}

If you observe the end of the line it should be 0.00. But I am getting as only 0. In out put I am getting like this LIN,1,1234567890123,EN,,,1.00,94.00,94.00,0

But this should be like below

LIN,1,1234567890123,EN,,,1.00,94.00,94.00,0.00

Any help would be greatly appreciated. Thank you.

1

There are 1 best solutions below

3
KIKO Software On

If I test your input, and leave numbers as numbers, I get:

LIN,1,1234567890123,EN,,,1,94,94,0

Observe how all whole numbers loose their zero's, which is not what you get. Your question seems inaccurate? The only reason, I can think of, why you get 94.00, is when it is a string. To illustrate this point I created this code:

$array = ['LIN', 1,1234567890123, 'EN', '', '', 1.00, '94.00', 94.00, '0.00'];
$out = fopen('php://output', 'w');
fputcsv($out, $array);
fclose($out);

And the output is:

LIN,1,1234567890123,EN,,,1,94.00,94,0.00 

So I see 2 solutions:

  1. Make numbers with trailing zero's into strings.
  2. Don't use fputcsv(), but make your own version.

And finally, do I need to point out that 0 is, numerically, the same as 0.00?