Documentation

Format extends AutoService
in package

Class Paths

Tags
service

format

This class provides standardised formatting

Table of Contents

Methods

getMoneyFormatter()  : NumberFormatter
Get the cached money formatter.
machineMoney()  : string
Get a number formatted to a machine format with padded decimal places.
money()  : string
Get formatted money
moneyLong()  : string
Get a money value with formatting but not rounding.
moneyNumber()  : string
Get a number formatted with rounding expectations derived from the currency.
moneyNumberLong()  : string
Get a number with minimum decimal places based on the currency but no rounding.
number()  : string
Get a formatted number.
isUseSeparatorSettings()  : bool
Should we use the configured thousand & decimal separators.
checkAndConvertAmount()  : string
Since the input can be various data types and values, we need to handle them before passing on to the Brick libraries which would throw exceptions for ones that we are ok just converting to the empty string.

Methods

getMoneyFormatter()

Get the cached money formatter.

public getMoneyFormatter([string|null $currency = NULL ][, string|null $locale = NULL ][, int $style = NumberFormatter::CURRENCY ][, array<string|int, mixed> $attributes = [] ]) : NumberFormatter
Parameters
$currency : string|null = NULL
$locale : string|null = NULL
$style : int = NumberFormatter::CURRENCY

See https://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants

$attributes : array<string|int, mixed> = []

See https://www.php.net/manual/en/class.numberformatter.php#intl.numberformatter-constants.unumberformatattribute

Tags
noinspection

PhpDocMissingThrowsInspection

noinspection

PhpUnhandledExceptionInspection

Return values
NumberFormatter

machineMoney()

Get a number formatted to a machine format with padded decimal places.

public machineMoney(string|float|int $amount[, string $currency = 'USD' ]) : string

This is intended to be a machine-friendly format that is also suitable for sending out to other systems that might expect 2 digits after the decimal point.

Most currencies format to 2 decimal places so the default of 'USD' will achieve that.

For example an input of 1000.1 will return 1000.10.

This will ensure that

Parameters
$amount : string|float|int
$currency : string = 'USD'
Tags
noinspection

PhpDocMissingThrowsInspection

noinspection

PhpUnhandledExceptionInspection

Return values
string

money()

Get formatted money

public money(string|int|float|BigDecimal $amount[, string|null $currency = NULL ][, string|null $locale = NULL ]) : string
Parameters
$amount : string|int|float|BigDecimal
$currency : string|null = NULL

Currency, defaults to site currency if not provided.

$locale : string|null = NULL
Tags
throws
CRM_Core_Exception
noinspection

PhpDocMissingThrowsInspection

noinspection

PhpUnhandledExceptionInspection

Return values
string

moneyLong()

Get a money value with formatting but not rounding.

public moneyLong(string|float|int $amount, string|null $currency, string|null $locale) : string
Parameters
$amount : string|float|int
$currency : string|null
$locale : string|null
Tags
noinspection

PhpDocMissingThrowsInspection

noinspection

PhpUnhandledExceptionInspection

Return values
string

moneyNumber()

Get a number formatted with rounding expectations derived from the currency.

public moneyNumber(string|float|int $amount, string $currency[, string|null $locale = NULL ]) : string
Parameters
$amount : string|float|int
$currency : string
$locale : string|null = NULL
Tags
noinspection

PhpDocMissingThrowsInspection

noinspection

PhpUnhandledExceptionInspection

Return values
string

moneyNumberLong()

Get a number with minimum decimal places based on the currency but no rounding.

public moneyNumberLong(string|float|int $amount, string|null $currency, string|null $locale) : string
Parameters
$amount : string|float|int
$currency : string|null
$locale : string|null
Tags
noinspection

PhpDocMissingThrowsInspection

noinspection

PhpUnhandledExceptionInspection

Return values
string

number()

Get a formatted number.

public number(string|int|float|Money $amount[, string|null $locale = NULL ][, array<string|int, mixed> $attributes = [NumberFormatter::MIN_FRACTION_DIGITS => 0, NumberFormatter::MAX_FRACTION_DIGITS => 8] ]) : string
Parameters
$amount : string|int|float|Money

Amount in a machine money format.

$locale : string|null = NULL
$attributes : array<string|int, mixed> = [NumberFormatter::MIN_FRACTION_DIGITS => 0, NumberFormatter::MAX_FRACTION_DIGITS => 8]

Additional values supported by NumberFormatter https://www.php.net/manual/en/class.numberformatter.php By default this will set it to round to 8 places and not add any padding.

Tags
throws
CRM_Core_Exception
Return values
string

isUseSeparatorSettings()

Should we use the configured thousand & decimal separators.

protected isUseSeparatorSettings() : bool

Historically, separators were configured with system-settings, but this is problematic when you have multiple locales with divergent separator rules (eg en_GB / fr_FR). The goal is to transition to locale-based-separators instead of setting-based-separators; but for now, we only switch if there's an opt-in flag.

Return values
bool

checkAndConvertAmount()

Since the input can be various data types and values, we need to handle them before passing on to the Brick libraries which would throw exceptions for ones that we are ok just converting to the empty string.

private checkAndConvertAmount(string|int|float|BigDecimal $amount) : string
Parameters
$amount : string|int|float|BigDecimal
Tags
throws
CRM_Core_Exception
Return values
string

Either the empty string if an empty-ish value, or the original amount as a string.


        
On this page

Search results