Skip to content

Commit

Permalink
Report: Cast floats explicitly on PHP < 8.0 in CSV exports
Browse files Browse the repository at this point in the history
fixes #122
  • Loading branch information
nilmerg committed Jan 24, 2023
1 parent 3a79758 commit 9134b7a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion library/Reporting/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public function toHtml()
public function toCsv()
{
$timerange = $this->getTimeframe()->getTimerange();
$convertFloats = version_compare(PHP_VERSION, '8.0.0', '<');

$csv = [];

Expand All @@ -314,7 +315,16 @@ public function toCsv()
$data = $implementation->getData($timerange, $reportlet->getConfig());
$csv[] = array_merge($data->getDimensions(), $data->getValues());
foreach ($data->getRows() as $row) {
$csv[] = array_merge($row->getDimensions(), $row->getValues());
$values = $row->getValues();
if ($convertFloats) {
foreach ($values as &$value) {
if (is_float($value)) {
$value = sprintf('%.4F', $value);
}
}
}

$csv[] = array_merge($row->getDimensions(), $values);
}

break;
Expand Down

0 comments on commit 9134b7a

Please sign in to comment.