-
Notifications
You must be signed in to change notification settings - Fork 0
Modules: Number
Alex Bruno Cáceres edited this page Mar 3, 2023
·
3 revisions
Useful methods to deal with numbers.
Formats a given number to a localized string.
It requires two arguments: number and locale. You can also pass in a third argument to set up the decimal length.
import { numberFormat } from '@jsweb/utils/modules/number'
const number = 1234567.89
const br = numberFormat(number, 'pt-BR') // 1.234.568
const en = numberFormat(number, 'en-US') // 1,234,568
const fr = numberFormat(number, 'fr-FR') // 1 234 568
const decimals = numberFormat(number, 'pt-BR', 2) // 1.234.567,89
Formats a given number to a localized currency string.
It requires three arguments: number, locale, and currency. You can also pass in a fourth argument to set up the decimal length.
import { currencyFormat } from '@jsweb/utils/modules/number'
const number = 1234567.89
const br = currencyFormat(number, 'pt-BR', 'BRL') // R$ 1.234.567,89
const en = currencyFormat(number, 'en-US', 'USD') // $1,234,567.89
const fr = currencyFormat(number, 'fr-FR', 'EUR') // 1 234 567,89 €
Checks if a given number is between two other numbers (inclusive).
import { isBetween } from '@jsweb/utils/modules/number'
const check = isBetween(256, 128, 512) // true