-1

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.

user3408779
  • 981
  • 2
  • 19
  • 43

1 Answers1

1

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?

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • Thank you, but the other side system will read this csv file. The last one is taxt rate. So they were asking tax rate as always 0.00. – user3408779 Feb 15 '22 at 07:04
  • 1
    @user3408779 Well, then make it into a string. See: [number_format($taxRate, 2);](https://www.php.net/manual/en/function.number-format). Do note that some tax rates may have more than 2 decimals. You might want to take that into account. – KIKO Software Feb 15 '22 at 07:06
  • @user3408779 Nice to hear it is solved. – KIKO Software Feb 15 '22 at 07:26