Montants en ariary
@mdgascar/ariary
Ce contenu n’est pas encore disponible dans votre langue.
Malagasy Ariary amounts: format, read, words and franc conversion. Part of malagasy-intl by Mdgascar.
Classes
Section titled “Classes”MalagasyIntlError
Section titled “MalagasyIntlError”Error thrown by malagasy-intl functions. Check code, which is the same in every language.
Example
Section titled “Example”try { spell(-1);} catch (error) { if (error instanceof MalagasyIntlError && error.code === 'OUT_OF_RANGE') { // handle it }}Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new MalagasyIntlError(
code,message):MalagasyIntlError
Creates an error with a shared spec code.
Parameters
Section titled “Parameters”Shared spec error code.
message
Section titled “message”string
Human-readable explanation, in English.
Returns
Section titled “Returns”Overrides
Section titled “Overrides”Error.constructor
Properties
Section titled “Properties”
readonlycode:ErrorCode
Why the call failed; the same code in every language.
readonlyname:"MalagasyIntlError"='MalagasyIntlError'
Always 'MalagasyIntlError'.
Overrides
Section titled “Overrides”Error.name
Methods
Section titled “Methods”[hasInstance]()
Section titled “[hasInstance]()”
static[hasInstance](value):boolean
Makes instanceof MalagasyIntlError true for errors from any malagasy-intl package or build.
Parameters
Section titled “Parameters”unknown
Any value, usually a caught error.
Returns
Section titled “Returns”boolean
true when value is an malagasy-intl error.
Remarks
Section titled “Remarks”Each package, and its ESM and CommonJS builds, has its own copy of this class, so the check looks at the error’s shape instead of its prototype chain.
Interfaces
Section titled “Interfaces”AmountResultOptions
Section titled “AmountResultOptions”Options for the functions that give back an amount.
Example
Section titled “Example”const options: AmountResultOptions = { as: 'text' };Properties
Section titled “Properties”
readonlyoptionalas?:AmountResultKind
number (default), or text for the exact decimal digits.
AriaryFormatOptions
Section titled “AriaryFormatOptions”Layout options for formatAriary and formatFmg.
Example
Section titled “Example”const options: AriaryFormatOptions = { grouping: 'dot', symbolPosition: 'after' };Extends
Section titled “Extends”Properties
Section titled “Properties”currencyDisplay?
Section titled “currencyDisplay?”
readonlyoptionalcurrencyDisplay?:AriaryCurrencyDisplay
symbol (default, Ar) or the ISO code (MGA).
decimal?
Section titled “decimal?”
readonlyoptionaldecimal?:AriaryDecimal
Decimal separator: comma (default), or dot for another convention.
Inherited from
Section titled “Inherited from”AriarySeparatorOptions.decimal
grouping?
Section titled “grouping?”
readonlyoptionalgrouping?:AriaryGrouping
Thousand separator: space (default), dot, comma for another convention, or none for
no separator at all (1500,5; reading then accepts only digits and one decimal mark).
Inherited from
Section titled “Inherited from”AriarySeparatorOptions.grouping
symbolPosition?
Section titled “symbolPosition?”
readonlyoptionalsymbolPosition?:AriarySymbolPosition
Symbol after (default) or before the amount.
AriaryParseOptions
Section titled “AriaryParseOptions”Options for reading amounts: which grouping and decimal separators the text uses, and
what to give back. Without grouping, every Malagasy one is accepted (spaces or dot);
decimal defaults to a comma. Say so for text in another convention:
{ grouping: 'comma', decimal: 'dot' }.
Example
Section titled “Example”const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };Extends
Section titled “Extends”Properties
Section titled “Properties”
readonlyoptionalas?:AmountResultKind
number (default), or text for the exact decimal digits of any amount.
decimal?
Section titled “decimal?”
readonlyoptionaldecimal?:AriaryDecimal
Decimal separator: comma (default), or dot for another convention.
Inherited from
Section titled “Inherited from”AriarySeparatorOptions.decimal
grouping?
Section titled “grouping?”
readonlyoptionalgrouping?:AriaryGrouping
Thousand separator: space (default), dot, comma for another convention, or none for
no separator at all (1500,5; reading then accepts only digits and one decimal mark).
Inherited from
Section titled “Inherited from”AriarySeparatorOptions.grouping
AriarySeparatorOptions
Section titled “AriarySeparatorOptions”Separator options for formatting and reading amounts.
Example
Section titled “Example”const english: AriarySeparatorOptions = { grouping: 'comma', decimal: 'dot' };Extended by
Section titled “Extended by”Properties
Section titled “Properties”decimal?
Section titled “decimal?”
readonlyoptionaldecimal?:AriaryDecimal
Decimal separator: comma (default), or dot for another convention.
grouping?
Section titled “grouping?”
readonlyoptionalgrouping?:AriaryGrouping
Thousand separator: space (default), dot, comma for another convention, or none for
no separator at all (1500,5; reading then accepts only digits and one decimal mark).
Type Aliases
Section titled “Type Aliases”AmountResultKind
Section titled “AmountResultKind”AmountResultKind =
"number"|"text"
What the functions that compute or read an amount give back: a number (default), or the
exact decimal text, of any size.
Example
Section titled “Example”const exact: AmountResultKind = 'text';AmountValue
Section titled “AmountValue”AmountValue<
Kind> =Kindextends"text"?string:number
The value an amount function gives back for an AmountResultKind: string for text,
otherwise number.
Type Parameters
Section titled “Type Parameters”Kind extends AmountResultKind
Example
Section titled “Example”const digits: AmountValue<'text'> = '1500.5';AriaryCurrencyDisplay
Section titled “AriaryCurrencyDisplay”AriaryCurrencyDisplay =
"symbol"|"code"
How the currency is shown: symbol (default, Ar) or the ISO 4217 code
(MGA; MGF for old francs).
Example
Section titled “Example”const display: AriaryCurrencyDisplay = 'code';AriaryDecimal
Section titled “AriaryDecimal”AriaryDecimal =
"comma"|"dot"
Decimal separator used by formatAriary and the parsers: comma (default) or dot.
AriaryGrouping
Section titled “AriaryGrouping”AriaryGrouping =
"space"|"dot"|"comma"|"none"
Thousand separator used by formatAriary: space (default), dot, comma or none.
AriarySymbolPosition
Section titled “AriarySymbolPosition”AriarySymbolPosition =
"before"|"after"
Where the currency symbol goes: after (default, 1 500 Ar) or before (Ar 1 500).
Example
Section titled “Example”const position: AriarySymbolPosition = 'after';ErrorCode
Section titled “ErrorCode”ErrorCode =
"INVALID_TYPE"|"OUT_OF_RANGE"|"INVALID_FORMAT"|"UNKNOWN_OPERATOR"|"PRECISION_LOSS"
Error code shared by every malagasy-intl language port.
INVALID_TYPE: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).OUT_OF_RANGE: The input has the right type but lies outside the supported range.INVALID_FORMAT: The input text cannot be read as a value of this module (wrong characters or wrong length).UNKNOWN_OPERATOR: The phone number is well formed but its operator code is not assigned to an operator.PRECISION_LOSS: The value is exact in the text but the port’s number type cannot hold it exactly; ask for the exact text instead.
MessageLocale
Section titled “MessageLocale”MessageLocale =
"en"|"fr"
Languages of the messages meant for end users.
SafeParseResult
Section titled “SafeParseResult”SafeParseResult<
T> = {data:T;success:true; } | {error:MalagasyIntlError;success:false; }
Outcome of a safeParse… function: the value, or the error that would have been thrown.
Type Parameters
Section titled “Type Parameters”T
Example
Section titled “Example”const result = safeParsePhone(input);if (result.success) console.log(result.data);else console.log(result.error.code);Variables
Section titled “Variables”SPEC_VERSION
Section titled “SPEC_VERSION”
constSPEC_VERSION:"0.16.0"='0.16.0'
Version of the shared spec this code was generated from.
Functions
Section titled “Functions”ariaryToFmg()
Section titled “ariaryToFmg()”ariaryToFmg<
Kind>(amount,options?):AmountValue<Kind>
Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
Type Parameters
Section titled “Type Parameters”Kind extends AmountResultKind = "number"
Parameters
Section titled “Parameters”amount
Section titled “amount”string | number | bigint
Ariary from 0 up, whole or decimal, as a number, a bigint or decimal text.
options?
Section titled “options?”Optional as: number (default) or text for the exact digits.
Kind
Returns
Section titled “Returns”AmountValue<Kind>
The amount in francs: 0.07 → 0.35, never 0.35000000000000003.
Throws
Section titled “Throws”MalagasyIntlError as fmgToAriary.
Example
Section titled “Example”ariaryToFmg(1500); // 7500describeError()
Section titled “describeError()”describeError(
error,locale?):string
A message for the end user of a form, in their language, for an error code or an error.
Parameters
Section titled “Parameters”ErrorCode | { code: ErrorCode; }
A MalagasyIntlError or its ErrorCode.
locale?
Section titled “locale?”MessageLocale = 'en'
en (default) or fr.
Returns
Section titled “Returns”string
A short sentence that fits next to a field.
Example
Section titled “Example”describeError('INVALID_FORMAT', 'fr'); // "Cette valeur n'est pas écrite dans un format lisible."fmgToAriary()
Section titled “fmgToAriary()”fmgToAriary<
Kind>(francs,options?):AmountValue<Kind>
Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
Type Parameters
Section titled “Type Parameters”Kind extends AmountResultKind = "number"
Parameters
Section titled “Parameters”francs
Section titled “francs”string | number | bigint
Francs from 0 up, whole or decimal, as a number, a bigint or decimal text.
options?
Section titled “options?”Optional as: number (default) or text for the exact digits.
Kind
Returns
Section titled “Returns”AmountValue<Kind>
The amount in ariary: 7501 → 1500.2.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for a value that is not a finite number or text,
INVALID_FORMAT for text that is not plain decimal, OUT_OF_RANGE for a negative number,
PRECISION_LOSS when a number cannot hold the result exactly.
Example
Section titled “Example”fmgToAriary(7500); // 1500fmgToAriary(7501); // 1500.2formatAmountInput()
Section titled “formatAmountInput()”formatAmountInput(
text,options?):string
Formats the text of an amount field as the user types it: digits grouped by thousands, the decimal separator kept, everything else dropped. It never throws a format error, so it can run on every keystroke.
Parameters
Section titled “Parameters”string
What has been typed so far.
options?
Section titled “options?”Optional grouping and decimal, as for formatAriary.
Returns
Section titled “Returns”string
The text to show in the field, '' when nothing usable was typed.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for options that do not exist or are not allowed.
Example
Section titled “Example”formatAmountInput('1500'); // '1 500'formatAmountInput('1500,'); // '1 500,'formatAmountInput('Ar 1500,5'); // '1 500,5'formatAriary()
Section titled “formatAriary()”formatAriary(
amount,options?):string
Writes an Ariary amount the Malagasy way: digits grouped by no-break spaces, then the symbol Ar.
Parameters
Section titled “Parameters”amount
Section titled “amount”string | number | bigint
Ariary from 0 to Number.MAX_SAFE_INTEGER (negative amounts too), whole or
decimal, a bigint, or decimal text such as '1500.25', written exactly.
options?
Section titled “options?”AriaryFormatOptions = {}
Optional grouping (space, dot or comma), decimal (comma or dot),
symbolPosition (after or before) and currencyDisplay (symbol or code). The defaults are the Malagasy way.
Returns
Section titled “Returns”string
The formatted amount, e.g. 1 500 Ar or 1 500,5 Ar (the spaces are U+00A0).
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for a value that is not a finite number or text, or
unknown options; INVALID_FORMAT for text that is not plain decimal; OUT_OF_RANGE for a
number outside 0 to Number.MAX_SAFE_INTEGER.
Example
Section titled “Example”formatAriary(1500); // '1 500 Ar'formatAriary(1500, { grouping: 'dot', symbolPosition: 'before' }); // 'Ar 1.500'formatAriary(1500.5); // '1 500,5 Ar'formatAriary(1500, { currencyDisplay: 'code' }); // '1 500 MGA'formatFmg()
Section titled “formatFmg()”formatFmg(
francs,options?):string
Writes an amount in old Malagasy francs, with the same layout as formatAriary.
Parameters
Section titled “Parameters”francs
Section titled “francs”string | number | bigint
Francs from 0 to Number.MAX_SAFE_INTEGER (negative too), whole or decimal,
a bigint, or decimal text.
options?
Section titled “options?”AriaryFormatOptions = {}
Same options as formatAriary.
Returns
Section titled “Returns”string
The formatted amount, e.g. 7 500 Fmg.
Throws
Section titled “Throws”MalagasyIntlError as formatAriary.
Example
Section titled “Example”formatFmg(7500); // '7 500 Fmg'parseAriary()
Section titled “parseAriary()”parseAriary<
Kind>(text,options?):AmountValue<Kind>
Reads an Ariary amount written in any common way.
Type Parameters
Section titled “Type Parameters”Kind extends AmountResultKind = "number"
Parameters
Section titled “Parameters”string
An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.
options?
Section titled “options?”AriarySeparatorOptions & object = {}
Optional grouping and decimal; see AriaryParseOptions.
Returns
Section titled “Returns”AmountValue<Kind>
The amount, whole or decimal.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE for non-strings or bad options, INVALID_FORMAT for text
that is not an amount (signs, a dot as decimal point by default), PRECISION_LOSS when a
number cannot hold the amount exactly (pass { as: 'text' } for the exact digits).
Example
Section titled “Example”parseAriary('Ar 1 500'); // 1500parseAriary('1.500 Ar'); // 1500parseAriary('1 500,50 Ar'); // 1500.5parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5safeAriaryToFmg()
Section titled “safeAriaryToFmg()”safeAriaryToFmg(…
args):SafeParseResult<string|number>
Like ariaryToFmg, but returns the error instead of throwing it.
Parameters
Section titled “Parameters”…[amount: string | number | bigint, options: object]
The arguments of ariaryToFmg.
Returns
Section titled “Returns”SafeParseResult<string | number>
{ success: true, data } or { success: false, error }.
Example
Section titled “Example”safeAriaryToFmg(1500); // { success: true, data: 7500 }safeAriaryToFmg(-1).success; // falsesafeFmgToAriary()
Section titled “safeFmgToAriary()”safeFmgToAriary(…
args):SafeParseResult<string|number>
Like fmgToAriary, but returns the error instead of throwing it.
Parameters
Section titled “Parameters”…[francs: string | number | bigint, options: object]
The arguments of fmgToAriary.
Returns
Section titled “Returns”SafeParseResult<string | number>
{ success: true, data } or { success: false, error }.
Example
Section titled “Example”safeFmgToAriary(7500); // { success: true, data: 1500 }safeFmgToAriary(-5).success; // falsesafeFormatAriary()
Section titled “safeFormatAriary()”safeFormatAriary(…
args):SafeParseResult<string>
Like formatAriary, but returns the error instead of throwing it.
Parameters
Section titled “Parameters”…[amount: string | number | bigint, options: AriaryFormatOptions]
The arguments of formatAriary.
Returns
Section titled “Returns”SafeParseResult<string>
{ success: true, data } or { success: false, error }.
Example
Section titled “Example”safeFormatAriary(1500); // { success: true, data: '1 500 Ar' }safeFormatAriary('abc').success; // falsesafeFormatFmg()
Section titled “safeFormatFmg()”safeFormatFmg(…
args):SafeParseResult<string>
Like formatFmg, but returns the error instead of throwing it.
Parameters
Section titled “Parameters”…[francs: string | number | bigint, options: AriaryFormatOptions]
The arguments of formatFmg.
Returns
Section titled “Returns”SafeParseResult<string>
{ success: true, data } or { success: false, error }.
Example
Section titled “Example”safeFormatFmg(7500); // { success: true, data: '7 500 Fmg' }safeFormatFmg('abc').success; // falsesafeParseAriary()
Section titled “safeParseAriary()”safeParseAriary(
text,options?):SafeParseResult<string|number>
Reads an Ariary amount without throwing.
Parameters
Section titled “Parameters”unknown
An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.
options?
Section titled “options?”AriaryParseOptions = {}
Optional grouping and decimal; see AriaryParseOptions.
Returns
Section titled “Returns”SafeParseResult<string | number>
{ success: true, data } with the amount, or { success: false, error }.
Example
Section titled “Example”const result = safeParseAriary('1 500 Ar');if (result.success) result.data; // 1500safeSpellAriary()
Section titled “safeSpellAriary()”safeSpellAriary(…
args):SafeParseResult<string>
Like spellAriary, but returns the error instead of throwing it.
Parameters
Section titled “Parameters”…[amount: string | number | bigint, options: SpellOptions]
The arguments of spellAriary.
Returns
Section titled “Returns”SafeParseResult<string>
{ success: true, data } or { success: false, error }.
Example
Section titled “Example”safeSpellAriary(1500); // { success: true, data: 'dimanjato sy arivo ariary' }safeSpellAriary(-1).success; // falsespellAriary()
Section titled “spellAriary()”spellAriary(
amount,options?):string
Writes an Ariary amount in Malagasy words.
Parameters
Section titled “Parameters”amount
Section titled “amount”string | number | bigint
Ariary from 0 to Number.MAX_SAFE_INTEGER, whole or decimal, a bigint, or
plain decimal text such as '1500.25', read exactly. Text is checked by spell (words
rule 13); a number by the Ariary range.
options?
Section titled “options?”SpellOptions = {}
The words system: standard (default) or traditional.
Returns
Section titled “Returns”string
The amount in words followed by ariary; decimals follow faingo.
Throws
Section titled “Throws”MalagasyIntlError INVALID_TYPE, INVALID_FORMAT (text) or OUT_OF_RANGE.
Example
Section titled “Example”spellAriary(1500); // 'dimanjato sy arivo ariary'spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'tryAriaryToFmg()
Section titled “tryAriaryToFmg()”tryAriaryToFmg(…
args):string|number|undefined
Like ariaryToFmg, but returns undefined instead of throwing.
Parameters
Section titled “Parameters”…[amount: string | number | bigint, options: object]
The arguments of ariaryToFmg.
Returns
Section titled “Returns”string | number | undefined
The value, or undefined.
Example
Section titled “Example”tryAriaryToFmg(1500); // 7500tryAriaryToFmg(-1); // undefinedtryFmgToAriary()
Section titled “tryFmgToAriary()”tryFmgToAriary(…
args):string|number|undefined
Like fmgToAriary, but returns undefined instead of throwing.
Parameters
Section titled “Parameters”…[francs: string | number | bigint, options: object]
The arguments of fmgToAriary.
Returns
Section titled “Returns”string | number | undefined
The value, or undefined.
Example
Section titled “Example”tryFmgToAriary(7500); // 1500tryFmgToAriary(-5); // undefinedtryFormatAriary()
Section titled “tryFormatAriary()”tryFormatAriary(…
args):string|undefined
Like formatAriary, but returns undefined instead of throwing.
Parameters
Section titled “Parameters”…[amount: string | number | bigint, options: AriaryFormatOptions]
The arguments of formatAriary.
Returns
Section titled “Returns”string | undefined
The value, or undefined.
Example
Section titled “Example”tryFormatAriary(1500); // '1 500 Ar'tryFormatAriary('abc'); // undefinedtryFormatFmg()
Section titled “tryFormatFmg()”tryFormatFmg(…
args):string|undefined
Like formatFmg, but returns undefined instead of throwing.
Parameters
Section titled “Parameters”…[francs: string | number | bigint, options: AriaryFormatOptions]
The arguments of formatFmg.
Returns
Section titled “Returns”string | undefined
The value, or undefined.
Example
Section titled “Example”tryFormatFmg(7500); // '7 500 Fmg'tryFormatFmg('abc'); // undefinedtryParseAriary()
Section titled “tryParseAriary()”tryParseAriary(
text,options?):string|number|undefined
Reads an Ariary amount, or gives undefined instead of throwing.
Use safeParseAriary when you need to know why the text was rejected.
Parameters
Section titled “Parameters”unknown
An amount such as Ar 1 500, 1.500 Ar, MGA 1500 or 1 500,50.
options?
Section titled “options?”AriaryParseOptions = {}
Optional grouping and decimal; see AriaryParseOptions.
Returns
Section titled “Returns”string | number | undefined
The amount, or undefined when parseAriary would throw.
Example
Section titled “Example”tryParseAriary('1.500 Ar'); // 1500tryParseAriary('1.5'); // undefinedtrySpellAriary()
Section titled “trySpellAriary()”trySpellAriary(…
args):string|undefined
Like spellAriary, but returns undefined instead of throwing.
Parameters
Section titled “Parameters”…[amount: string | number | bigint, options: SpellOptions]
The arguments of spellAriary.
Returns
Section titled “Returns”string | undefined
The value, or undefined.
Example
Section titled “Example”trySpellAriary(1500); // 'dimanjato sy arivo ariary'trySpellAriary(-1); // undefinedReferences
Section titled “References”NumberSystem
Section titled “NumberSystem”Re-exports NumberSystem
SpellOptions
Section titled “SpellOptions”Re-exports SpellOptions