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).
At a glance
Section titled “At a glance”formatAriary(1500)Ar 1 500formatAriary(2026000)Ar 2 026 000formatAriary(1500.5)Ar 1 500,5
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 |
spellAriary(amount, options) | 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(...), Ariary::fromText($text) | Amount → Ar 1 500 (grouping, symbol position) |
Ariary::parse($text, ...)->amount(), ->digits | 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)->spell(...), Ariary::spellText($text, ...) | Amount → Malagasy words + ariary |
Ariary::fromFmg($francs)->amount(), ->digits | Old francs → Ariary (5 FMG = 1 Ar) |
Ariary::fromInt($amount)->toFmg(), ->toFmgDigits() | 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.
| Case | Call | Result |
|---|---|---|
| default | formatAriary(1500.5) | Ar 1 500,5 |
| dot grouping | formatAriary(1500.5, { grouping: 'dot' }) | Ar 1.500,5 |
| symbol after | formatAriary(1500.5, { symbolPosition: 'after' }) | 1 500,5 Ar |
| ISO code | formatAriary(1500.5, { currencyDisplay: 'code' }) | MGA 1 500,5 |
| English style | formatAriary(1500.5, { grouping: 'comma', decimal: 'dot' }) | Ar 1,500.5 |
| no grouping | formatAriary(1500.5, { grouping: 'none' }) | Ar 1500,5 |
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'formatAriary(1500, { currencyDisplay: 'code' }); // 'MGA 1 500'use Mdgascar\MalagasyIntl\Ariary\Ariary;use Mdgascar\MalagasyIntl\Ariary\CurrencyDisplay;use Mdgascar\MalagasyIntl\Ariary\Grouping;use Mdgascar\MalagasyIntl\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'Ariary::fromInt(1500)->format(currency: CurrencyDisplay::Code); // 'MGA 1 500'Separators
Section titled “Separators”How amounts are written and read, in one place:
Writing (formatAriary) |
Reading (parseAriary) |
|
|---|---|---|
| Thousands | A no-break space by default (Ar 1 500), or a dot (Ar 1.500) |
A space, a no-break space, a narrow no-break space or a dot, the same one throughout; groups of three |
| Decimals | A comma by default (Ar 1 500,50) |
A comma by default: 1,500 is 1.5 Ar |
| Currency | Ar before the amount by default, or after it, or the code MGA |
Ar, MGA or Ariary, before or after, or none |
| No grouping | Opt-in: { grouping: 'none' } writes Ar 1500,50 |
The same option reads only plain numbers: 1500,50 |
| English style | Opt-in: { grouping: 'comma', decimal: 'dot' } writes Ar 1,500.50 |
The same options read 1,500.50 |
| Rejected | Signs, leading zeros, other currencies, mixed separators, groups of the wrong size |
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.
How typed text is read
Section titled “How typed text is read”By default, with no options:
- Thousands: spaces (any kind) or a dot, in groups of three digits:
1 500,1.500. - Decimals: a comma:
1 500,50,1,5. - Anything else is an error, never a guess.
1.5is not 1.5 Ar and not 1500 Ar: it isINVALID_FORMAT.
| Call | Result |
|---|---|
safeParseAriary('1 500') | 1500 |
safeParseAriary('1.500') | 1500 |
safeParseAriary('1 500,50') | 1500.5 |
safeParseAriary('1,5') | 1.5 |
safeParseAriary('1,500') | 1.5 |
safeParseAriary('1.5') | ✗ INVALID_FORMAT |
To read other conventions, choose the separators: grouping ('space', 'dot', 'comma' or 'none') and decimal ('comma' or 'dot'). Given a grouping, only that character groups thousands.
| Call | Result |
|---|---|
safeParseAriary('1.5', { decimal: 'dot' }) | 1.5 |
safeParseAriary('1500.5', { decimal: 'dot' }) | 1500.5 |
safeParseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }) | 1500.5 |
safeParseAriary('1 500,50', { grouping: 'space' }) | 1500.5 |
safeParseAriary('1.500,50', { grouping: 'space' }) | ✗ INVALID_FORMAT |
safeParseAriary('1500,5', { grouping: 'none' }) | 1500.5 |
safeParseAriary('1 500', { grouping: 'none' }) | ✗ INVALID_FORMAT |
safeParseAriary('1.5', { grouping: 'none', decimal: 'dot' }) | 1.5 |
With grouping: 'none', only digits and one decimal mark are accepted: the strictest reading, for fields where people type a plain number.
All the options
Section titled “All the options”| Option | Values | Formatting (formatAriary) |
Reading (parseAriary) |
|---|---|---|---|
grouping |
'space' (default), 'dot', 'comma', 'none' |
The thousands separator written | The only thousands separator accepted; without it, spaces and dots |
decimal |
'comma' (default), 'dot' |
The decimal mark written | The decimal mark accepted |
symbolPosition |
'before' (default), 'after' |
Ar 1 500 or 1 500 Ar |
Not an option: both positions are read |
currencyDisplay |
'symbol' (default), 'code' |
Ar or MGA |
Not an option: Ar, MGA and Ariary are read |
as |
'number' (default), 'text' |
A number, or the exact digits as text |
grouping and decimal must be different characters, otherwise the call throws INVALID_TYPE. An unknown option is INVALID_TYPE too.
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 MalagasyIntlError INVALID_TYPE: same as the decimal commause Mdgascar\MalagasyIntl\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 MalagasyIntlException, 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 { spellAriary } from '@mdgascar/ariary';
spellAriary(1500); // 'dimanjato sy arivo ariary'spellAriary(25000); // 'dimy arivo sy roa alina ariary'spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'Ariary::fromInt(1500)->spell(); // 'dimanjato sy arivo ariary'Ariary::fromInt(25000)->spell(); // 'dimy arivo sy roa alina ariary'Ariary::fromFloat(1500.5)->spell(); // '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'Never rounded
Section titled “Never rounded”Reading and converting never round. When a number cannot hold the amount exactly, you get PRECISION_LOSS; ask for the exact digits as text instead.
parseAriary('1 500,50 Ar', { as: 'text' }); // '1500.5'formatAriary('1500.25'); // 'Ar 1 500,25' (text in, written exactly)Ariary::parse('1 500,50 Ar')->digits; // '1500.5'Ariary::fromText('1500.25')->format(); // 'Ar 1 500,25'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 | OUT_OF_RANGE |
| A number that cannot hold the amount exactly | PRECISION_LOSS |