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).

  • formatAriary(1500)Ar 1 500
  • formatAriary(2026000)Ar 2 026 000
  • formatAriary(1500.5)Ar 1 500,5

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
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

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.

Results computed by the library
CaseCallResult
defaultformatAriary(1500.5)Ar 1 500,5
dot groupingformatAriary(1500.5, { grouping: 'dot' })Ar 1.500,5
symbol afterformatAriary(1500.5, { symbolPosition: 'after' })1 500,5 Ar
ISO codeformatAriary(1500.5, { currencyDisplay: 'code' })MGA 1 500,5
English styleformatAriary(1500.5, { grouping: 'comma', decimal: 'dot' })Ar 1,500.5
no groupingformatAriary(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'

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

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.

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.5 is not 1.5 Ar and not 1500 Ar: it is INVALID_FORMAT.
Results computed by the library
CallResult
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.

Results computed by the library
CallResult
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.

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.

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 MalagasyIntlError INVALID_TYPE: same as the decimal comma

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'

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'

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)
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