Skip to content

DecimalFormatter

The decimal formatter will format numbers according to a given format.

The format is close to the one used by ICU. It consists of the following elements

class DecimalFormatter implements ResetInterface

ImplementsResetInterface
Since1.0.0
SourceUtil/DecimalFormatter.php
ConstantValueDescription
CURRENCY_CODE2
CURRENCY_NAME3
CURRENCY_SYMBOL1
IN_NUMBER2
IN_POSTFIX3
IN_PREFIX1
ROUND_CEIL4
ROUND_FINANCIAL2
ROUND_FLOOR3
ROUND_NONE0
ROUND_SCIENTIFIC1

public function __construct(string $format = null): mixed

Constructs a new Decimalformatter with the optional format.

The format (if any).

ParameterTypeDescription
$formatstringThe format (if any).

Returns mixed

MethodDescription
[`formatCurrency(intfloat $number, string $currencySymbol): string`](#formatcurrency)
[`formatNumber(intfloat
getFormat(): ?stringReturns the format which is currently used to format numbers.
getRoundingMode(): intReturns the rounding mode.
getRoundingModeFromString(string $mode): intMaps a string rounding mode definition to the rounding mode constants.
[`parse(string $string, QuioteLocalestring
reset(): voidClears the parsed number format so the formatter can be re-configured.
setFormat(string $format): voidSets the format to be used for formatting numbers.
setRoundingMode(int $mode): voidSets the rounding mode.

public function formatCurrency(int|float $number, string $currencySymbol): string

Formats the given currency and returns the formatted result.

The currency symbol to be used when formatting.

ParameterTypeDescription
$number`int““float`
$currencySymbolstringThe currency symbol to be used when formatting.

Returns string — The currency formatted in the desired format.

public function formatNumber(int|float|string $number): string

Formats the given number and returns the formatted result.

The number to be formatted.

ParameterTypeDescription
$number`int“float

Returns string — The number formatted in the desired format.

public function getFormat(): ?string

Returns the format which is currently used to format numbers.

Returns ?``string — The current format.

public function getRoundingMode(): int

Returns the rounding mode.

Returns int — The rounding mode.

public function getRoundingModeFromString(string $mode): int

Maps a string rounding mode definition to the rounding mode constants.

The mode string.

ParameterTypeDescription
$modestringThe mode string.

Returns int — The rounding mode constant.

public static function parse(string $string, QuioteLocale|string|null $locale = null, bool &$hasExtraChars = false): mixed

Parses a string into float or int.

An out value indicating whether there were additional characters after the matched number.

ParameterTypeDescription
$stringstringThe input number string.
$localeQuioteLocale`string
$hasExtraCharsboolAn out value indicating whether there were additional characters after the matched number.

Returns mixed — The result if parsing was successful or false when the input was no number.

public function reset(): void

Clears the parsed number format so the formatter can be re-configured.

Everything derived from the format string — separators, grouping distances, digit counts, sign and currency flags — goes back to its default. The rounding mode is left alone, as the comment below records.

public function setFormat(string $format): void

Sets the format to be used for formatting numbers.

The format.

ParameterTypeDescription
$formatstringThe format.

public function setRoundingMode(int $mode): void

Sets the rounding mode.

The rounding mode.

ParameterTypeDescription
$modeintThe rounding mode.