Last active
June 11, 2023 14:49
-
-
Save ChristianOellers/eb4fc964a07ff093861381af4e26b542 to your computer and use it in GitHub Desktop.
Currency for Locale - Render-Optimized (avoid NaN). Example use: React.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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