Formatting Numbers as Currency Strings in JavaScript
To format a price in JavaScript as a currency string, such as "$ 2,500.00", you can utilize the Intl.NumberFormat function provided by the Internationalization API.
Implementation:
const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', });
const formattedPrice = formatter.format(amount);
Customization Options:
You can further customize the formatting by utilizing additional parameters, such as:
By leveraging Intl.NumberFormat, you can easily format numbers as currency strings in a locale-aware manner.
The above is the detailed content of How Can I Format Numbers as Currency Strings in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!