Novis\Intl\NumberFormat::percent
Novis\Intl\NumberFormat::percent(
array<int|float|decimal> $numbers,
string $locale,
?DigitOptions $options = null,
): array<string>Writes numbers as percentages, the way a language and region write them.
NumberFormat::percent returns one string for each number, in the same order as the list. The number is multiplied by 100, so 0.25 is 25% and 1 is 100%. The locale decides how the result looks. English writes 25%. German writes 25 %, with a space before the sign.
By default the result has no fraction digits, so 0.125 is 13%. The options are the same as for NumberFormat::decimal: minFractionDigits, maxFractionDigits and grouping.
NumberFormat::percent throws a RuntimeError when a float is not finite. It throws a LogicError when the locale tag or an option is not valid.
Good to know: pass the share, not the percentage. For 25 percent, pass 0.25, not 25.
Parameters
$numbersarray<int|float|decimal>$localestring$options?DigitOptionsdefault null
Return value
Returns array<string>.
Examples
A share as a percentage
<?nvs// Shows how to write the share of finished orders as a percentage.use Novis\Intl\NumberFormat;
int $finished = 3;int $orders = 4;
// 3 of 4 is 0.75. This prints 75%.echo NumberFormat::percent([$finished / $orders], 'en')[0], "\n";75%The locale changes the sign
<?nvs// Shows that each language writes the percent sign in its own way.use Novis\Intl\NumberFormat;
array<int|float|decimal> $discounts = [0.1, 0.25];
// English writes the sign right after the number. This prints 10% | 25%.echo Core\Str::join(NumberFormat::percent($discounts, 'en'), ' | '), "\n";
// German puts a space before the sign. This prints 10 % | 25 %.echo Core\Str::join(NumberFormat::percent($discounts, 'de'), ' | '), "\n";10% | 25%10 % | 25 %One fraction digit
<?nvs// Shows how to keep one fraction digit in a percentage.use Novis\Intl\NumberFormat;
array<int|float|decimal> $rates = [0.1234, 0.5];
// By default there are no fraction digits. This prints 12% and 50%.echo Core\Str::join(NumberFormat::percent($rates, 'en'), ' and '), "\n";
// With one digit, this prints 12.3% and 50.0%.echo Core\Str::join(NumberFormat::percent($rates, 'en', { minFractionDigits: 1, maxFractionDigits: 1 }), ' and '), "\n";12% and 50%12.3% and 50.0%