Skip to content
GitHub

Modules

Ariary amounts

Format amounts in Malagasy Ariary (MGA), whole or decimal, read amounts typed by users, write them in words and convert old Malagasy francs (FMG).

Type an amount the way people write it. The result shows it formatted and in words.

Examples

Result

Ar 2 026 000 · enina arivo sy roa alina sy roa tapitrisa ariary

JSformatAriary(parseAriary('Ar 2 026 000'), { grouping: 'space' })PHPAriary::parse('Ar 2 026 000')->format(Grouping::Space)
Open in the playground →
formatAriary(amount, options)Amount → Ar 1 500 (grouping, symbol position)
parseAriary(text, options)Text → amount, or throws
safeParseAriary(text, options)Text → amount or error code, never throws
tryParseAriary(text, options) → undefinedText → amount, or nothing, never throws
ariaryToWords(amount)Amount → Malagasy words + ariary
fmgToAriary(francs)Old francs → Ariary (5 FMG = 1 Ar)
ariaryToFmg(amount)Ariary → old francs
formatFmg(francs, options)Francs → Fmg 7 500

Amounts are numbers, whole or decimal, with no upper limit in the rules. Thousands are grouped with a no-break space (or a dot), and decimals follow a comma. A comma is never a thousand separator.

import { formatAriary } from '@mdgascar/ariary';
formatAriary(1500); // 'Ar 1 500'
formatAriary(1500.5); // 'Ar 1 500,5'
formatAriary(2026000, { grouping: 'dot' }); // 'Ar 2.026.000'
formatAriary(1234567.89, { grouping: 'dot' }); // 'Ar 1.234.567,89'
formatAriary(1500, { symbolPosition: 'after' }); // '1 500 Ar'

Ar 1 500, 1.500 Ar, MGA 1500 and 1 500 all read as 1500, and 1 500,50 Ar as 1500.5. Signs are rejected, and so is a dot used as a decimal point.

Examples

Result

Ar 1 500,5 · dimanjato sy arivo faingo dimy ariary

JSformatAriary(parseAriary('1 500,50 Ar'), { grouping: 'space' })PHPAriary::parse('1 500,50 Ar')->format(Grouping::Space)
Open in the playground →
import { parseAriary, safeParseAriary, tryParseAriary } from '@mdgascar/ariary';
parseAriary('1.500 Ar'); // 1500
parseAriary('MGA 1500'); // 1500
parseAriary('1 500,50 Ar'); // 1500.5
parseAriary('1,500'); // 1.5 (the comma is the decimal separator)
tryParseAriary('1.5'); // undefined: a dot is not a decimal point by default
const result = safeParseAriary('1.5');
if (!result.success) result.error.code; // 'INVALID_FORMAT'

Every option defaults to the Malagasy way. For text written in another convention, pass grouping and decimal: the library never guesses. The two must be different characters.

formatAriary(1500.5, { grouping: 'comma', decimal: 'dot' }); // 'Ar 1,500.5'
parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
parseAriary('1,500', { grouping: 'comma', decimal: 'dot' }); // 1500
formatAriary(1500, { grouping: 'comma' }); // throws MdgError INVALID_TYPE: same as the decimal comma

For receipts and cheques: the amount in Malagasy words followed by ariary. Decimals follow faingo.

import { ariaryToWords } from '@mdgascar/ariary';
ariaryToWords(1500); // 'dimanjato sy arivo ariary'
ariaryToWords(25000); // 'dimy arivo sy roa alina ariary'
ariaryToWords(1500.5); // 'dimanjato sy arivo faingo dimy ariary'

5 FMG = 1 Ar. The conversion is exact in decimal, so francs that do not divide by 5 give decimal ariary, never a rounding artefact.

import { ariaryToFmg, fmgToAriary, formatFmg } from '@mdgascar/ariary';
fmgToAriary(7500); // 1500
fmgToAriary(7501); // 1500.2
ariaryToFmg(1500); // 7500
ariaryToFmg(0.07); // 0.35 (exact, never 0.35000000000000003)
formatFmg(7500); // 'Fmg 7 500'
formatFmg(7500.5); // 'Fmg 7 500,5'
Input Error code
Not a finite number, bad options, or the same character for grouping and decimal INVALID_TYPE
Text with signs, a dot as decimal point (by default) or another currency INVALID_FORMAT
Below 0, or above the largest exact number of the language OUT_OF_RANGE