Currency (BRL)

Formatting, parsing and writing out in words amounts in reais.

  • Parity matrix

Format

Formats a number in the BRL pattern 1.234,56.

  • A number keeps its sign and its decimals. options.symbol (default false) prefixes the result with R$ (-R$ 1,50 for a negative one). options.precision sets the decimal places (default 2, clamped to 0 to 20; a precision that is not a finite number is 2).
  • The function reads a string as currency.parse reads it, except that a value without any separator is whole units (1234 formats as 1.234,00).
  • A string with no digit reads as 0: abc and the empty string format as 0,00, and null also gives 0,00. A value that is not a finite number (NaN, infinite) or cannot be coerced to a number (a symbol, a plain object) returns an empty string. Any other non-string value goes through Number(), so true formats as 1,00 and [] as 0,00.

Pending decision

The reference (JS) adds no currency symbol by default (the R$ prefix is opt-in through options.symbol). The other libraries prefix R$ . See the open decision in docs/findings.md.

Pending decision

For a non-finite or non-numeric value, the reference (JS) returns an empty string. Other libraries return null. See the open decision in docs/findings.md.

ParameterTypeRequired
valuestring | numberyes
optionsFormatCurrencyOptionsno
options.symbolbooleanno
options.precisionnumberno
returnsstring

Format a number or a numeric string in the BRL pattern (1.234,56). A number is formatted as is, sign and decimals preserved.

  • Options (FormatCurrencyOptions): symbol (default false) prefixes the result with R$; precision (default 2) sets the decimal places, clamped to 0 to 20.
  • A string is read as parseCurrency reads it, except that a value without any separator stays in whole units: '1234' formats as 1.234,00.
  • Returns '' for a non-finite value or one that cannot be coerced to a number. A string is read by parseCurrency, so a string with no digit reads as 0 and formats as 0,00 ('abc'), and null also gives 0,00.
import { formatCurrency } from '@brazilian-utils/brazilian-utils';

formatCurrency(10); // 10,00
formatCurrency(10756.11); // 10.756,11
formatCurrency(10756.123, { precision: 3 }); // 10.756,123
formatCurrency(1234.56, { symbol: true }); // R$ 1.234,56
formatCurrency(-1050); // -1.050,00 (a number's sign is preserved)
formatCurrency('123456'); // 123.456,00 (a plain digit string is read as a whole number)
formatCurrency('1.234,56'); // 1.234,56 (the last "," or "." followed by 1 to 2 digits is the decimal separator)
formatCurrency('-10.5'); // -10,50 (a leading "-" is preserved)
formatCurrency(Number.NaN); // "" (non finite numbers format as an empty string)

Source: Lei nº 9.069/1995, art. 1º, which sets the R$ symbol and the comma before the centavos. Based on: the CLDR pt-BR locale data behind Intl.NumberFormat, for the . grouping.

Code: brazilian-utils/javascript
Try it with JavaScript formatCurrency
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (76) and the result in each library currency.format

Parse

Parses a BRL amount string (such as R$ 1.234,56) into a number.

  • The last , or . followed by 1 to 2 digits (up to options.precision, when larger) is the decimal separator. Every other , or . is a thousands separator.
  • The function reads a value without any separator as cents (divided by 10 to the power of the precision, default 2).
  • Only a - before the first digit makes the result negative (-R$ 1,00 is -1). An accounting negative such as (R$ 1,00) or a trailing sign as in 1,00- parses to 1. An empty string is 0.
  • Every character that is not a digit or a separator is dropped, so 1e5 reads as the digits 15 and parses to 0.15.
  • options.precision (default 2, clamped to 0 to 20) is the number of digits read as minor units: the divisor for a value without separator and the largest decimal group. A precision that is not a finite number is 2.
ParameterTypeRequired
valuestringyes
optionsParseCurrencyOptionsno
options.precisionnumberno
returnsnumber

Parse a BRL currency string into a number.

  • Options (ParseCurrencyOptions): precision (default 2) is the number of digits read as minor units, clamped to 0 to 20.
  • The last , or . followed by 1 to 2 digits (up to precision, when larger) is the decimal separator; every other , or . is a thousands separator.
  • A value without any separator is read as cents and divided by 10 ** precision.
  • Only a - before the first digit makes the result negative: '(R$ 1,00)' and '1,00-' parse to 1, and characters that are not digits or separators are dropped, so '1e5' parses to 0.15.
import { parseCurrency } from '@brazilian-utils/brazilian-utils';

parseCurrency('R$ 1.234,56'); // 1234.56
parseCurrency('1234,56'); // 1234.56
parseCurrency('R$ 0,50'); // 0.5
parseCurrency('R$ 1.234'); // 1234 ("." followed by 3 digits is a thousands separator)
parseCurrency('1,5'); // 1.5
parseCurrency('1234'); // 12.34 (no separator at all keeps the cents convention)
parseCurrency('-R$ 1,00'); // -1 (a leading "-" is preserved)
parseCurrency('R$ 1,001', { precision: 3 }); // 1.001
parseCurrency(''); // 0

Source: Lei nº 9.069/1995, art. 1º, which sets the R$ symbol and the comma before the centavos. Based on: the CLDR pt-BR locale data behind Intl.NumberFormat, for the . grouping.

Code: brazilian-utils/javascript
Try it with JavaScript parseCurrency
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (39) and the result in each library currency.parse

Write out in words

Writes an amount in reais in Brazilian Portuguese words ("por extenso"), as on checks and contracts.

For example, 1523.45 becomes "mil quinhentos e vinte e três reais e quarenta e cinco centavos".

  • The function truncates value (does not round it) to 2 decimal places.
  • Singular and plural agree ("um real", "um centavo", "zero reais"). Every other amount takes the plural ("dois reais", "um milhão e um reais").
  • The function adds the prefix "menos" to a negative amount, except when the amount truncates to nothing: -0.001 is "zero reais".
  • The function takes no options. A round million, billion or trillion of reais takes "de" ("um milhão de reais"), and the centavos, when there are any, follow ("um bilhão de reais e cinquenta centavos").
  • Above about 90 trillion reais (Number.MAX_SAFE_INTEGER / 100) a number cannot hold cents, so the amount is read as whole reais. The cents are read off the decimal notation of the value, so floating point noise does not change them (1.15 is "um real e quinze centavos").

Pending decision

The reference (JS) writes all lower case with no comma between groups. Several libraries capitalize the first word and some add commas. See the open decision in docs/findings.md.

Pending decision

For invalid input or an amount above 999 trillion reais, the reference (JS) returns an empty string. See the open decision in docs/findings.md.

ParameterTypeRequired
valuenumberyes
returnsstring

Write an amount in reais in words ("por extenso"), as on cheques and contracts: 1523.45 becomes "mil quinhentos e vinte e três reais e quarenta e cinco centavos". Takes no options.

  • value is truncated (not rounded) to 2 decimal places.
  • Returns "" for invalid input or an amount above 999 trillion reais.
  • The singular is used for exactly one ("um real", "um centavo"), and a round million, billion or trillion of reais takes "de": "um milhão de reais".
  • An amount that truncates to nothing is "zero reais", even when negative (-0.001); any other negative amount is prefixed with "menos".
  • Above about 90 trillion reais (Number.MAX_SAFE_INTEGER / 100) a number cannot hold cents, so the amount is read as whole reais.
import { convertCurrencyToWords } from '@brazilian-utils/brazilian-utils';

convertCurrencyToWords(1523.45); // "mil quinhentos e vinte e três reais e quarenta e cinco centavos"
convertCurrencyToWords(1); // "um real"
convertCurrencyToWords(0.01); // "um centavo"
convertCurrencyToWords(1000000); // "um milhão de reais"
convertCurrencyToWords(0); // "zero reais"
convertCurrencyToWords(-5.5); // "menos cinco reais e cinquenta centavos"
convertCurrencyToWords(-0.001); // "zero reais" (truncates to nothing)
Code: brazilian-utils/javascript
Try it with JavaScript convertCurrencyToWords
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (31) and the result in each library currency.convertToWords

Official sources

See also Numbers in words

Last updated on

On this page