Oihana PHP Standards

NumberFormat uses ConstantsTrait

Common number formatting separators and symbols.

Provides the building blocks needed to format numbers per regional conventions. The constants are designed to be passed straight to PHP's number_format() (decimal separator + thousands separator), but can be used anywhere a single separator or symbol is needed.

Notation summary:

  • EU: comma decimal, dot thousands — 1.234.567,89 (DE, IT, ES, NL…)
  • US: dot decimal, comma thousands — 1,234,567.89 (US, UK, JP, CN…)
  • FR: comma decimal, narrow no-break space thousands — 1 234 567,89 (FR, per typographic rules)
  • CH: dot decimal, apostrophe thousands — 1'234'567.89 (Switzerland)

Example usage:

use org\common\NumberFormat;

echo number_format(1234567.89, 2,
    NumberFormat::DECIMAL_SEP_EU,
    NumberFormat::THOUSANDS_SEP_EU
); // "1.234.567,89"

echo number_format(1234567.89, 2,
    NumberFormat::DECIMAL_SEP_FR,
    NumberFormat::THOUSANDS_SEP_FR
); // "1 234 567,89" (with narrow no-break space)

NumberFormat::includes(',');         // true
NumberFormat::getConstant('%');      // "PERCENT_SYMBOL"
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.2

Table of Contents

Constants

DECIMAL_SEP_CH  : string = '.'
Decimal separator — Swiss convention: dot (`.`).
DECIMAL_SEP_EU  : string = ','
Decimal separator — European convention: comma (`,`).
DECIMAL_SEP_FR  : string = ','
Decimal separator — French convention: comma (`,`).
DECIMAL_SEP_US  : string = '.'
Decimal separator — US/UK/Asia convention: dot (`.`).
INFINITY  : string = '∞'
Infinity symbol (`∞`, U+221E).
NAN_SYMBOL  : string = 'NaN'
"Not a Number" symbol (`NaN`).
NEGATIVE_INFINITY  : string = '-∞'
Negative infinity (`-∞`).
PERCENT_SYMBOL  : string = '%'
Percent sign (`%`).
PERMILLE_SYMBOL  : string = '‰'
Per-mille sign (`‰`, U+2030).
SCIENTIFIC_E_LOWER  : string = 'e'
Scientific notation exponent marker, lowercase (`e`), as in `1.23e+45`.
SCIENTIFIC_E_UPPER  : string = 'E'
Scientific notation exponent marker, uppercase (`E`), as in `1.23E+45`.
THOUSANDS_SEP_CH  : string = "'"
Thousands separator — Swiss convention: apostrophe (`'`).
THOUSANDS_SEP_EU  : string = '.'
Thousands separator — European convention: dot (`.`).
THOUSANDS_SEP_FR  : string = " "
Thousands separator — French convention: narrow no-break space (U+202F).
THOUSANDS_SEP_NONE  : string = ''
No thousands separator (empty string).
THOUSANDS_SEP_US  : string = ','
Thousands separator — US/UK/Asia convention: comma (`,`).

Constants

DECIMAL_SEP_CH

Decimal separator — Swiss convention: dot (`.`).

public string DECIMAL_SEP_CH = '.'

DECIMAL_SEP_EU

Decimal separator — European convention: comma (`,`).

public string DECIMAL_SEP_EU = ','

DECIMAL_SEP_FR

Decimal separator — French convention: comma (`,`).

public string DECIMAL_SEP_FR = ','

DECIMAL_SEP_US

Decimal separator — US/UK/Asia convention: dot (`.`).

public string DECIMAL_SEP_US = '.'

INFINITY

Infinity symbol (`∞`, U+221E).

public string INFINITY = '∞'

NAN_SYMBOL

"Not a Number" symbol (`NaN`).

public string NAN_SYMBOL = 'NaN'

NEGATIVE_INFINITY

Negative infinity (`-∞`).

public string NEGATIVE_INFINITY = '-∞'

PERCENT_SYMBOL

Percent sign (`%`).

public string PERCENT_SYMBOL = '%'

PERMILLE_SYMBOL

Per-mille sign (`‰`, U+2030).

public string PERMILLE_SYMBOL = '‰'

SCIENTIFIC_E_LOWER

Scientific notation exponent marker, lowercase (`e`), as in `1.23e+45`.

public string SCIENTIFIC_E_LOWER = 'e'

SCIENTIFIC_E_UPPER

Scientific notation exponent marker, uppercase (`E`), as in `1.23E+45`.

public string SCIENTIFIC_E_UPPER = 'E'

THOUSANDS_SEP_CH

Thousands separator — Swiss convention: apostrophe (`'`).

public string THOUSANDS_SEP_CH = "'"

THOUSANDS_SEP_EU

Thousands separator — European convention: dot (`.`).

public string THOUSANDS_SEP_EU = '.'

THOUSANDS_SEP_FR

Thousands separator — French convention: narrow no-break space (U+202F).

public string THOUSANDS_SEP_FR = " "

THOUSANDS_SEP_NONE

No thousands separator (empty string).

public string THOUSANDS_SEP_NONE = ''

THOUSANDS_SEP_US

Thousands separator — US/UK/Asia convention: comma (`,`).

public string THOUSANDS_SEP_US = ','
On this page

Search results