PersianNumerals
extends NumberLocalizer
in package
Converts integers to and from Persian number words.
Table of Contents
Properties
- $textNormalizer : TextNormalizer
- Text normalizer applied before parsing numbers.
Methods
- __construct() : mixed
- Creates a Persian numeral localizer.
- formatCardinal() : string
- Formats an integer as Persian cardinal words.
- formatDigits() : string
- Formats an integer as a string of Persian digits, with an optional minimum number of digits padded with leading zeros.
- formatOrdinal() : string
- Formats a non-zero integer as Persian ordinal words.
- parse() : NumberParseResult
- Parses an integer from localized digits or words.
- parseCardinal() : int|null
- Parses an integer from localized cardinal words.
- parseDigits() : int|null
- Parses Persian, Arabic, or Latin digits into an integer.
- parseOrdinal() : int|null
- Parses an integer from localized ordinal words.
- parseWords() : NumberParseResult
- Parses Persian number words into an integer.
Properties
$textNormalizer read-only
Text normalizer applied before parsing numbers.
protected
TextNormalizer
$textNormalizer
Methods
__construct()
Creates a Persian numeral localizer.
public
__construct([TextNormalizer|null $textNormalizer = null ]) : mixed
Parameters
- $textNormalizer : TextNormalizer|null = null
-
Text normalizer, or null to use Persian normalization.
formatCardinal()
Formats an integer as Persian cardinal words.
public
formatCardinal(int $value) : string
Parameters
- $value : int
-
Integer to format.
Tags
Return values
string —Persian cardinal word representation.
formatDigits()
Formats an integer as a string of Persian digits, with an optional minimum number of digits padded with leading zeros.
public
formatDigits(int $value[, int $minimumDigits = 1 ]) : string
Parameters
- $value : int
-
Integer to format.
- $minimumDigits : int = 1
-
Minimum number of digits to include, padded with leading zeros if necessary.
Tags
Return values
string —The formatted string of Persian digits.
formatOrdinal()
Formats a non-zero integer as Persian ordinal words.
public
formatOrdinal(int $value) : string
Positive values are counted from the start of a sequence. Negative values are counted from the end of a sequence.
Parameters
- $value : int
-
Non-zero integer to format.
Tags
Return values
string —Persian ordinal word representation.
parse()
Parses an integer from localized digits or words.
public
parse(string $text) : NumberParseResult
Digit parsing is tried before word parsing.
Parameters
- $text : string
-
Number representation to parse.
Tags
Return values
NumberParseResult —Parse result.
parseCardinal()
Parses an integer from localized cardinal words.
public
parseCardinal(string $text) : int|null
Parameters
- $text : string
-
Cardinal words to parse.
Tags
Return values
int|null —Parsed integer, or null when invalid or not cardinal.
parseDigits()
Parses Persian, Arabic, or Latin digits into an integer.
public
parseDigits(string $text) : int|null
Invalid inputs return null.
Parameters
- $text : string
-
Digit representation to parse.
Tags
Return values
int|null —The parsed integer, or null if the input cannot be parsed as a valid number.
parseOrdinal()
Parses an integer from localized ordinal words.
public
parseOrdinal(string $text) : int|null
Parameters
- $text : string
-
Ordinal words to parse.
Tags
Return values
int|null —Parsed integer, or null when invalid or not ordinal.
parseWords()
Parses Persian number words into an integer.
public
parseWords(string $text) : NumberParseResult
A valid input is a Persian cardinal number such as "صفر", "چهل و دو", or "یک میلیون و دو هزار و سه". Components must appear in descending numeric order, large groups must be used from larger to smaller, and the conjunction "و" must separate adjacent components where Persian number grammar requires it. Whitespace and ZWNJ are both accepted as token separators, so forms such as "بیستویک" and "سی ام" are accepted. Arabic variants of ی and ک are normalized before parsing.
The final component may be an ordinal or superlative ordinal form, such as "یکم", "اول", "سیام", or "یکمین". The forms "اول" and "نخست" are accepted only when they are the whole ordinal; compound first ordinals must use the regular "یکم" form, such as "بیست و یکم". The words "آخر" and "آخرین", and phrases such as "دوم از آخر", are parsed as ordinal positions counted from the end, resulting in negative integer values.
A leading "منفی" is accepted for cardinal numbers only, including zero, but not for ordinals.
Parameters
- $text : string
-
The string to parse.
Tags
Return values
NumberParseResult —Parse result.