Docs/Formatters

Formatters

Money and number formatting shared by every Ajax UI finance component - Intl-based, locale-aware, and built on the minor-units contract.


Import

import { formatCents, formatCurrency, formatPercent } from "@ajax-ui/react/format";

The minor-units contract

Monetary amounts travel as integers in the currency's smallest unit (cents for BRL/USD/EUR, whole yen for JPY). Integer math never loses precision; floats appear only at the display boundary, where Intl.NumberFormat rounds correctly. Components that accept money (MoneyInput and friends) follow the same rule.

API & live output

CallRenders
formatCents(123456)R$ 1.234,56
formatCents(999, { currency: "JPY", locale: "ja-JP" })¥999
formatCents(-4550, { signDisplay: "always" })-R$ 45,50
formatCurrency(1234.56, { currency: "USD", locale: "en-US" })$1,234.56
formatPercent(0.153)15,3%
formatPercent(-0.021, { signDisplay: "always" })-2,1%
  • formatCents(cents, options?) - formats an integer amount of minor units.
  • formatCurrency(amount, options?) - formats a major-unit decimal you already hold.
  • formatPercent(ratio, options?) - formats a ratio (0.153 → 15,3%).
  • getCurrencyFractionDigits(currency, locale?) - how many fraction digits the currency uses.
  • parseDigitsToMinorUnits(text) - strips user-typed text to an integer amount of minor units.
  • Options: currency (default "BRL"), locale (default "pt-BR"), signDisplay.