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).
Try it
Section titled “Try it”Type an amount the way people write it. The result shows it formatted and in words.
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)Quick reference
Section titled “Quick reference”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) → undefined | Text → 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 |
Ariary::fromInt($amount)->format(...) | Amount → Ar 1 500 (grouping, symbol position) |
Ariary::parse($text, ...)->amount | Text → amount, or throws |
Ariary::safeParse($text, ...) | Text → amount or error code, never throws |
Ariary::tryParse($text, ...) → null | Text → amount, or nothing, never throws |
Ariary::fromInt($amount)->toWords() | Amount → Malagasy words + ariary |
Ariary::fromFmg($francs)->amount | Old francs → Ariary (5 FMG = 1 Ar) |
Ariary::fromInt($amount)->toFmg() | Ariary → old francs |
Ariary::formatFmg($francs, ...) | Francs → Fmg 7 500 |
Formatting
Section titled “Formatting”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'use Mdgascar\MdgUtils\Ariary\Ariary;use Mdgascar\MdgUtils\Ariary\Grouping;use Mdgascar\MdgUtils\Ariary\SymbolPosition;
Ariary::fromInt(1500)->format(); // 'Ar 1 500'Ariary::fromFloat(1500.5)->format(); // 'Ar 1 500,5'Ariary::fromInt(2026000)->format(Grouping::Dot); // 'Ar 2.026.000'Ariary::fromFloat(1234567.89)->format(Grouping::Dot); // 'Ar 1.234.567,89'Ariary::fromInt(1500)->format(position: SymbolPosition::After); // '1 500 Ar'Reading amounts typed by users
Section titled “Reading amounts typed by users”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.
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)import { parseAriary, safeParseAriary, tryParseAriary } from '@mdgascar/ariary';
parseAriary('1.500 Ar'); // 1500parseAriary('MGA 1500'); // 1500parseAriary('1 500,50 Ar'); // 1500.5parseAriary('1,500'); // 1.5 (the comma is the decimal separator)
tryParseAriary('1.5'); // undefined: a dot is not a decimal point by defaultconst result = safeParseAriary('1.5');if (!result.success) result.error.code; // 'INVALID_FORMAT'Ariary::parse('1.500 Ar')->amount; // 1500Ariary::parse('MGA 1500')->amount; // 1500Ariary::parse('1 500,50 Ar')->amount; // 1500.5Ariary::parse('1,500')->amount; // 1.5 (the comma is the decimal separator)
Ariary::tryParse('1.5'); // null: a dot is not a decimal point by defaultAriary::safeParse('1.5')->error?->errorCode; // ErrorCode::InvalidFormatOther conventions
Section titled “Other conventions”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.5parseAriary('1,500', { grouping: 'comma', decimal: 'dot' }); // 1500formatAriary(1500, { grouping: 'comma' }); // throws MdgError INVALID_TYPE: same as the decimal commause Mdgascar\MdgUtils\Ariary\DecimalSeparator;
Ariary::fromFloat(1500.5)->format(Grouping::Comma, decimal: DecimalSeparator::Dot); // 'Ar 1,500.5'Ariary::parse('1,500.50', Grouping::Comma, DecimalSeparator::Dot)->amount; // 1500.5Ariary::parse('1,500', Grouping::Comma, DecimalSeparator::Dot)->amount; // 1500Ariary::fromInt(1500)->format(Grouping::Comma); // throws MdgUtilsException, ErrorCode::InvalidTypeAmounts in words
Section titled “Amounts in words”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'Ariary::fromInt(1500)->toWords(); // 'dimanjato sy arivo ariary'Ariary::fromInt(25000)->toWords(); // 'dimy arivo sy roa alina ariary'Ariary::fromFloat(1500.5)->toWords(); // 'dimanjato sy arivo faingo dimy ariary'Old Malagasy francs (FMG)
Section titled “Old Malagasy francs (FMG)”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); // 1500fmgToAriary(7501); // 1500.2ariaryToFmg(1500); // 7500ariaryToFmg(0.07); // 0.35 (exact, never 0.35000000000000003)formatFmg(7500); // 'Fmg 7 500'formatFmg(7500.5); // 'Fmg 7 500,5'Ariary::fromFmg(7500)->amount; // 1500Ariary::fromFmg(7501)->amount; // 1500.2Ariary::fromInt(1500)->toFmg(); // 7500Ariary::fromFloat(0.07)->toFmg(); // 0.35 (exact)Ariary::formatFmg(7500); // 'Fmg 7 500'Ariary::formatFmg(7500.5); // 'Fmg 7 500,5'Errors
Section titled “Errors”| 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 |