satya
satya

Reputation: 1949

format of number on downloading in csv format

Following code returns hours for given seconds, which is working.

<?php

$hrs = seconds_to_dezi(278280);
echo $hrs;
$filename = "test";
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=$filename.xls");

function seconds_to_dezi($s) {
    $n = false;
    if ($s < 0) {
        $n = true;
        $s = -$s;
    }
    $h = round($s / 3600,2);
    $h = number_format($h,2, ',', ' ');
    $h = str_replace(".",",",$h);


    if ($n == true) {
        return ("-" . $h);
    } else {
        return ($h);
    }
}

?>

Actual output in excel:- 77,3

Expected output is:- 77,30

i.e on downloading values in csv format, the trailing zero after decimal is truncated. I want to have that zero in excel.

Upvotes: 0

Views: 1312

Answers (2)

Hugo Delsing
Hugo Delsing

Reputation: 14163

You cannot explicitly set the number of decimals in a CSV file. If you need to show 77,30 you can use ="70,30". It will show up in the file correct, but it will be text then. So you can no longer use it as a number in excel.

Upvotes: 0

Alin P.
Alin P.

Reputation: 44346

The function is returning 77,30. The thing is that Excel doesn't show the final 0, but it's there. Open the file with a text editor.

Upvotes: 1

Related Questions