Skip to main content
100%

Hook that formats a number as a currency using the browser's locale.

✓ Published0🌍 Public
Aabonello
Last edited Jul 29, 2021
Created on Dec 30, 2021

This example shows a React hook that formats a numeric value as a currency string according to the browser’s locale. It uses the `Intl.NumberFormat` API to apply locale-specific formatting, and maps the locale’s country code to a currency code (e.g., `IN` to `INR`, `JP` to `JPY`) via a lookup table, defaulting to `USD` when no match exists. The hook relies on `useMemo` to cache both the formatter and the formatted output, recomputing only when the locale or value changes. It takes a `value` and an optional `locale` (defaulting to `navigator.language`) and returns the formatted currency string.

AI-generated description

Similar vizzes