DEV Community

Formatting Monetary Values in JavaScript

When building web applications, you may need to format numbers as monetary values, such as displaying 123.45 as £123.45. This article explores two approaches to formatting monetary values in JavaScript. The first approach uses the toLocaleString() method, which formats a number according to the specified locale and options. For example, you can use toLocaleString('en-GB', {style: 'currency', currency: 'GBP'}) to format a value as a British pound sterling. You can also customize the precision of the formatted values by using the minimumFractionDigits and maximumFractionDigits options. The second approach uses the Intl.NumberFormat object, which allows you to create a reusable formatter for formatting multiple values. You can customize the precision of the formatted values by specifying the minimumFractionDigits and maximumFractionDigits options when creating the formatter. This article demonstrates how to format monetary values in JavaScript using toLocaleString() and Intl.NumberFormat. The article concludes by suggesting further reading materials and inviting readers to sign up for the newsletter.
favicon
dev.to
dev.to