public function formatMetrics(DataTable $dataTable, Report $report = null, $metricsToFormat = null, $formatAll = false)
    {
        $metrics = $this->getMetricsToFormat($dataTable, $report);
        if (empty($metrics)
            || $dataTable->getMetadata(self::PROCESSED_METRICS_FORMATTED_FLAG)
        ) {
            return;
        }

        $dataTable->setMetadata(self::PROCESSED_METRICS_FORMATTED_FLAG, true);

        if ($metricsToFormat !== null) {
            $metricMatchRegex = $this->makeRegexToMatchMetrics($metricsToFormat);
            $metrics = array_filter($metrics, function ($metric) use ($metricMatchRegex) {
                /** @var ProcessedMetric|ArchivedMetric $metric */
                return preg_match($metricMatchRegex, $metric->getName());
            });
        }

        foreach ($metrics as $name => $metric) {
            if (!$metric->beforeFormat($report, $dataTable)) {
                continue;
            }

            foreach ($dataTable->getRows() as $row) {
                $columnValue = $row->getColumn($name);
                if ($columnValue !== false) {
                    $row->setColumn($name, $metric->format($columnValue, $this));
                }
            }
        }

        foreach ($dataTable->getRows() as $row) {
            $subtable = $row->getSubtable();
            if (!empty($subtable)) {
                $this->formatMetrics($subtable, $report, $metricsToFormat);
            }
        }

        $idSite = DataTableFactory::getSiteIdFromMetadata($dataTable);
        if (empty($idSite)) {
            // possible when using search in visualization
            $idSite = Common::getRequestVar('idSite', 0, 'int');
        }

        // @todo for matomo 4, should really use the Metric class to house this kind of logic
        // format other metrics
        if ($formatAll) {
            foreach ($dataTable->getRows() as $row) {
                foreach ($row->getColumns() as $column => $columnValue) {
                    if (strpos($column, 'revenue') === false
                        || !is_numeric($columnValue)
                    ) {
                        continue;
                    }

                    if ($columnValue !== false) {
                        $row->setColumn($column, $this->getPrettyMoney($columnValue, $idSite));
                    }
                }
            }
        }
    }