Changeset View
Changeset View
Standalone View
Standalone View
src/app/Utils.php
Show First 20 Lines • Show All 600 Lines • ▼ Show 20 Lines | class Utils | ||||
* A helper to display human-readable percent value | * A helper to display human-readable percent value | ||||
* for specified currency and locale. | * for specified currency and locale. | ||||
* | * | ||||
* @param int|float $percent Percent value (0 to 100) | * @param int|float $percent Percent value (0 to 100) | ||||
* @param string $locale Output locale | * @param string $locale Output locale | ||||
* | * | ||||
* @return string String representation, e.g. "0 %", "7.7 %" | * @return string String representation, e.g. "0 %", "7.7 %" | ||||
*/ | */ | ||||
public static function percent(int|float $amount, $locale = 'de_DE'): string | public static function percent(int|float $percent, $locale = 'de_DE'): string | ||||
{ | { | ||||
$nf = new \NumberFormatter($locale, \NumberFormatter::PERCENT); | $nf = new \NumberFormatter($locale, \NumberFormatter::PERCENT); | ||||
$sep = $nf->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); | $sep = $nf->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL); | ||||
$result = sprintf('%.2f', $amount); | $result = sprintf('%.2f', $percent); | ||||
$result = preg_replace('/\.00/', '', $result); | $result = preg_replace('/\.00/', '', $result); | ||||
$result = preg_replace('/(\.[0-9])0/', '\\1', $result); | $result = preg_replace('/(\.[0-9])0/', '\\1', $result); | ||||
$result = str_replace('.', $sep, $result); | $result = str_replace('.', $sep, $result); | ||||
return $result . ' %'; | return $result . ' %'; | ||||
} | } | ||||
} | } |