Skip to content

Core\Math::format

Core\Math::format(
    int|float|decimal $n,
    {decimals?: uint, decimalSeparator?: string, groupSeparator?: string},
): string

Core\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: an int, a float or a decimal.

Options

  • decimalsuintoptionalHow many digits to write after the decimal separator, from 0 to 100. The default is 0, 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 $n is INFINITY, -INFINITY or NaN, or when decimals is more than 100.
  • ArithmeticErrorWhen $n is a uint larger than Core\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";
Output
3
3.14
42.000
0.00

Separators

<?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";
Output
1234567.89
1,234,567.89
1.234.567,89
1 234 567,89

Price 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";
Output
$19.90
$1,250.00
$0.50
Total: $1,270.40