Core\Math::format
Core\Math::format(
int|float|decimal $n,
{decimals?: uint, decimalSeparator?: string, groupSeparator?: string},
): stringCore\Math::format writes a number as text for a person to read. You choose how many decimals to write, the text between the whole part and the decimals, and the text between groups of three digits.
The number can be an int, a float or a decimal. It is rounded to the number of decimals you ask for, and a half is rounded away from zero, so 2.5 is written as 3. The digits are not grouped unless you give a groupSeparator. A negative number starts with -, and a number that rounds to zero has no -.
A float cannot store most decimal fractions exactly. 1.005 is stored as a number slightly below it, so it is written as 1.00 with two decimals. Use a decimal when the digits must be exact, such as for money.
INFINITY and NaN have no digits, so they throw a RuntimeError.
The examples below show decimals and rounding, then separators for different countries, then a price list.
Parameters
$nint|float|decimalThe number to write: anint, afloator adecimal.
Options
decimalsuintoptionalHow many digits to write after the decimal separator, from0to100. The default is0, which writes no decimal separator.decimalSeparatorstringoptionalThe text between the whole part and the decimals. The default is..groupSeparatorstringoptionalThe text between each group of three digits, counted from the right. The default is empty, so the digits are not grouped.
Return value
Returns string. The text. The number is rounded to decimals places, and a half is rounded away from zero. A negative number starts with -. A number that rounds to zero has no -.
Errors
RuntimeErrorWhen$nisINFINITY,-INFINITYorNaN, or whendecimalsis more than100.ArithmeticErrorWhen$nis auintlarger thanCore\Math::INT_MAX.
Examples
Decimals and rounding
<?nvs// Shows how to write a number with a fixed number of decimals.
// With no options, the number is rounded to a whole number. This prints 3.echo Core\Math::format(2.5), "\n";
// `decimals` sets how many digits come after the `.`. This prints 3.14.echo Core\Math::format(3.14159, { decimals: 2 }), "\n";
// Missing decimals are filled with zeros. This prints 42.000.echo Core\Math::format(42, { decimals: 3 }), "\n";
// A negative number that rounds to zero has no `-`. This prints 0.00.echo Core\Math::format(-0.001, { decimals: 2 }), "\n";33.1442.0000.00Separators
<?nvs// Shows how to write one number the way different countries write it.
float $amount = 1234567.891;
// With no `groupSeparator`, the digits are not grouped. This prints 1234567.89.echo Core\Math::format($amount, { decimals: 2 }), "\n";
// This prints 1,234,567.89, as in the United States.echo Core\Math::format($amount, { decimals: 2, groupSeparator: ',' }), "\n";
// This prints 1.234.567,89, as in Germany.echo Core\Math::format($amount, { decimals: 2, decimalSeparator: ',', groupSeparator: '.' }), "\n";
// This prints 1 234 567,89, as in France.echo Core\Math::format($amount, { decimals: 2, decimalSeparator: ',', groupSeparator: ' ' }), "\n";1234567.891,234,567.891.234.567,891 234 567,89Price list
<?nvs// Shows how a shop writes its prices and the total of an order.
class Price{ // Returns the price as text with two decimals and a comma between thousands. public static function text(decimal $amount): string { return '$' . Core\Math::format($amount, { decimals: 2, groupSeparator: ',' }); }}
array<decimal> $prices = [19.9, 1250.0, 0.5];decimal $total = 0.0;foreach ($prices as decimal $price) { $total = $total + $price; // This prints $19.90, then $1,250.00, then $0.50. echo Price::text($price), "\n";}// This prints Total: $1,270.40.echo 'Total: ', Price::text($total), "\n";$19.90$1,250.00$0.50Total: $1,270.40