Skip to content

Instantly share code, notes, and snippets.

@ChristianOellers
Last active June 11, 2023 14:49
Show Gist options
  • Select an option

  • Save ChristianOellers/eb4fc964a07ff093861381af4e26b542 to your computer and use it in GitHub Desktop.

Select an option

Save ChristianOellers/eb4fc964a07ff093861381af4e26b542 to your computer and use it in GitHub Desktop.
Currency for Locale - Render-Optimized (avoid NaN). Example use: React.
/**
* Format currency for locale (which can come from anywhere).
*
* Ignore nullish values (like undefined) to avoid showing 'NaN €' as text, to keep templating simple.
* Example use case: First render occurs prior to data being available (e.g. in React).
*/
const locale = 'de-DE'
export default (val) => {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: 'EUR'
}).format(val ?? 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment