<?php
 namespace App\Utils; class PriceFormat { static public function format(float|string|null $amount, ?string $currency=null, $hideCurrency=false, $humanReadable=false) :string { $newAmount = (float)$amount; $exp = ''; if ($humanReadable) { $amountAbs = abs($newAmount); if ($amountAbs >= 1000000000) { $newAmount = round($newAmount / 1000000000, 2); $exp = 'b'; } elseif ($amountAbs >= 1000000) { $newAmount = round($newAmount / 1000000, 2); $exp = 'm'; } elseif ($amountAbs >= 1000) { $newAmount = round($newAmount / 1000, 2); $exp = 'k'; } } $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); if ($currency) { $decimals = \App\Config\CurrencyDecimals::getCurrencyDecimals($currency); if ($decimals===null) { $decimals = $CFG['localization']['maxDecimalsOnPrices']; } } else { $decimals = $CFG['localization']['maxDecimalsOnPrices']; } if ($CFG['localization']['numberFormat']=='eu') { $decimalsSep = ','; $thousandsSep = '.'; } elseif ($CFG['localization']['numberFormat']=='us') { $decimalsSep = '.'; $thousandsSep = ','; } else { $decimalsSep = '.'; $thousandsSep = ''; } $formattedAmount = number_format((float)$newAmount, $decimals, $decimalsSep, $thousandsSep); if ($hideCurrency) { return $formattedAmount . $exp; } else { if ($CFG['localization']['priceFormat']=='currencyFirst') { return trim($currency . ' ' . $formattedAmount . $exp); } else { return trim($formattedAmount . $exp . ' ' . $currency); } } } }