Malagasy Intl
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)1 500 ArformatAriary(2026000)2 026 000 ArformatAriary(1500.5)1 500,5 Ar
Try it
Section titled “Try it”Type an amount the way people write it. The result shows it formatted and in words.
Result
2 026 000 Ar · 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 → 1 500 Ar (grouping, symbol position) |
formatAmountInput(text, options) | Text being typed → grouped (rule 7) |
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 → 1 500 Ar (grouping, symbol position) |
Ariary::formatInput($text, $grouping, $decimal) | Text being typed → grouped (rule 7) |
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.
1 500,5 Ar
- 1 500digits in groups of three
- ,5decimals, after a comma
- Arsymbol
| Case | Call | Result |
|---|---|---|
| default | formatAriary(1500.5) | 1 500,5 Ar |
| dot grouping | formatAriary(1500.5, { grouping: 'dot' }) | 1.500,5 Ar |
| symbol after | formatAriary(1500.5, { symbolPosition: 'after' }) | 1 500,5 Ar |
| ISO code | formatAriary(1500.5, { currencyDisplay: 'code' }) | 1 500,5 MGA |
| English style | formatAriary(1500.5, { grouping: 'comma', decimal: 'dot' }) | 1,500.5 Ar |
| no grouping | formatAriary(1500.5, { grouping: 'none' }) | 1500,5 Ar |
import { formatAriary } from '@mdgascar/ariary';
formatAriary(1500); // '1 500 Ar'formatAriary(1500.5); // '1 500,5 Ar'formatAriary(2026000, { grouping: 'dot' }); // '2.026.000 Ar'formatAriary(1234567.89, { grouping: 'dot' }); // '1.234.567,89 Ar'formatAriary(1500, { symbolPosition: 'after' }); // '1 500 Ar'formatAriary(1500, { currencyDisplay: 'code' }); // '1 500 MGA'use Mdgascar\MalagasyIntl\Ariary\Ariary;use Mdgascar\MalagasyIntl\Ariary\CurrencyDisplay;use Mdgascar\MalagasyIntl\Ariary\Grouping;use Mdgascar\MalagasyIntl\Ariary\SymbolPosition;
Ariary::fromInt(1500)->format(); // '1 500 Ar'Ariary::fromFloat(1500.5)->format(); // '1 500,5 Ar'Ariary::fromInt(2026000)->format(Grouping::Dot); // '2.026.000 Ar'Ariary::fromFloat(1234567.89)->format(Grouping::Dot); // '1.234.567,89 Ar'Ariary::fromInt(1500)->format(position: SymbolPosition::After); // '1 500 Ar'Ariary::fromInt(1500)->format(currency: CurrencyDisplay::Code); // '1 500 MGA'Separators
Section titled “Separators”How amounts are written and read, in one place:
Writing (formatAriary) |
Reading (parseAriary) |
|
|---|---|---|
| Thousands | A no-break space by default (1 500 Ar), or a dot (1.500 Ar) |
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 (1 500,50 Ar) |
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 1500,50 Ar |
The same option reads only plain numbers: 1500,50 |
| English style | Opt-in: { grouping: 'comma', decimal: 'dot' } writes 1,500.50 Ar |
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 |
'after' (default), 'before' |
1 500 Ar or Ar 1 500 |
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
1 500,5 Ar · 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' }); // '1,500.5 Ar'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); // '1,500.5 Ar'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'Negative amounts
Section titled “Negative amounts”A negative amount takes the minus sign right before the digits: a refund, a balance, a debt. It reads back the same way. Amounts in words and FMG conversions stay from 0.
| Call | Result |
|---|---|
formatAriary(-1500) | -1 500 Ar |
formatAriary(-1500, { symbolPosition: 'before' }) | Ar -1 500 |
formatAriary('-0.5') | -0,5 Ar |
| Call | Result |
|---|---|
safeParseAriary('-1 500 Ar') | -1500 |
safeParseAriary('Ar -1 500') | -1500 |
safeParseAriary('- 1 500') | ✗ INVALID_FORMAT |
safeParseAriary('1 500-') | ✗ INVALID_FORMAT |
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.35formatFmg(7500); // '7 500 Fmg'formatFmg(7500.5); // '7 500,5 Fmg'Ariary::fromFmg(7500)->amount(); // 1500Ariary::fromFmg(7501)->amount(); // 1500.2Ariary::fromInt(1500)->toFmg(); // 7500Ariary::fromFloat(0.07)->toFmg(); // 0.35 (exact)Ariary::formatFmg(7500); // '7 500 Fmg'Ariary::formatFmg(7500.5); // '7 500,5 Fmg'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'); // '1 500,25 Ar' (text in, written exactly)Ariary::parse('1 500,50 Ar')->digits; // '1500.5'Ariary::fromText('1500.25')->format(); // '1 500,25 Ar'While the user types
Section titled “While the user types”formatAmountInput groups an amount field as it is typed, with the same separators as formatAriary. It keeps only digits and the decimal separator and never reads the amount, so it never throws for the text.
import { formatAmountInput } from '@mdgascar/ariary';
formatAmountInput('1500'); // '1 500'formatAmountInput('Ar 1500,'); // '1 500,'Ariary::formatInput('1500'); // '1 500'Ariary::formatInput('Ar 1500,'); // '1 500,'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 |