Currency (BRL)
Formatting, parsing and writing out in words amounts in reais.
Format
- JavaScript library
- Python library37 cases fail
- Go library4 cases fail
- Ruby library15 cases fail
- Rust library
- .NET library3 cases fail
- Erlang library
Formats a number in the BRL pattern 1.234,56.
- A number keeps its sign and its decimals.
options.symbol(defaultfalse) prefixes the result withR$(-R$ 1,50for a negative one).options.precisionsets 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.parsereads it, except that a value without any separator is whole units (1234formats as1.234,00). - A string with no digit reads as 0:
abcand the empty string format as0,00, andnullalso gives0,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 throughNumber(), sotrueformats as1,00and[]as0,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.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
options | FormatCurrencyOptions | no |
options.symbol | boolean | no |
options.precision | number | no |
| returns | string |
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(defaultfalse) prefixes the result withR$;precision(default 2) sets the decimal places, clamped to 0 to 20. - A
stringis read asparseCurrencyreads it, except that a value without any separator stays in whole units:'1234'formats as1.234,00. - Returns
''for a non-finite value or one that cannot be coerced to a number. A string is read byparseCurrency, so a string with no digit reads as0and formats as0,00('abc'), andnullalso gives0,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.
Try it with JavaScript formatCurrency
Shared test cases (76) and the result in each library currency.format
Parse
- JavaScript library
- Python library
- Go library4 cases fail
- Ruby library3 cases fail
- Rust library1 case fails
- .NET library5 cases fail
- Erlang library
Parses a BRL amount string (such as R$ 1.234,56) into a number.
- The last
,or.followed by 1 to 2 digits (up tooptions.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,00is -1). An accounting negative such as(R$ 1,00)or a trailing sign as in1,00-parses to 1. An empty string is 0. - Every character that is not a digit or a separator is dropped, so
1e5reads as the digits15and 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.
| Parameter | Type | Required |
|---|---|---|
value | string | yes |
options | ParseCurrencyOptions | no |
options.precision | number | no |
| returns | number |
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 toprecision, 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 to1, and characters that are not digits or separators are dropped, so'1e5'parses to0.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(''); // 0Source: 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.
Try it with JavaScript parseCurrency
Shared test cases (39) and the result in each library currency.parse
Write out in words
- JavaScript library
- Python library31 cases fail
- Go library7 cases fail
- Ruby library2 cases fail
- Rust library6 cases fail
- .NET library5 cases fail
- Erlang library
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.001is "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.15is "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.
| Parameter | Type | Required |
|---|---|---|
value | number | yes |
| returns | string |
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.
valueis 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)Try it with JavaScript convertCurrencyToWords
Shared test cases (31) and the result in each library currency.convertToWords
Official sources
See also Numbers in words
Last updated on
