以csv格式下载的数字格式

时间:2011-04-12 13:20:00

标签: php

以下代码返回给定秒数的小时数,这是有效的。

<?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);
    }
}

?>

excel中的实际输出: - 77,3

预期产出为: - 77,30

即以csv格式下载值时,截断小数后的尾随零。我想在excel中获得零。

2 个答案:

答案 0 :(得分:2)

该功能返回77,30。问题是Excel没有显示最终的0,但它就在那里。使用文本编辑器打开文件。

答案 1 :(得分:0)

您无法在CSV文件中明确设置小数位数。如果你需要显示77,30你可以使用=“70,30”。它将在文件中显示正确,但它将是文本。因此,您不能再将其用作excel中的数字。