# Formatting numbers the Right Way

**URL:** <https://discourse.nodered.org/t/formatting-numbers-the-right-way/83928>\
**Category:** FAQs\
**Tags:** javascript\
**Created:** [21 December 2023 15:31 UTC](https://discourse.nodered.org/t/formatting-numbers-the-right-way/83928 "2023-12-21T15:31:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 December 2023 15:31 UTC](https://discourse.nodered.org/t/formatting-numbers-the-right-way/83928/1 "2023-12-21T15:31:56Z")

</div>

Node.JS and modern browsers all support the [INTL](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) library natively. That library has lots of features for formatting things to a locale specific format including dates and numbers.

So here is a cheeky little JavaScript function that you should be able to use either in a Node-RED function node or in custom browser code:

```javascript
function dp(inp, dp, int) {
    if (!int) int = 'en-GB'
    if (!dp) dp = 1
    return inp.toLocaleString(int, { 'minimumFractionDigits': dp, 'maximumFractionDigits': dp })
}

```

Called like: `msg.payload = dp(msg.payload)` It would convert an input number like `12345678.12345` to an output string like "12,345,678.1". If you were to use a European format, the comma and dot would be reversed.

Of course, as shown, it only specifies the locale to use and the number of decimal places to show (it rounds by default but that can also be changed). You can do lots of tweaks to this and there are some pre-defined number formats such as currency.

> **[Number.prototype.toLocaleString() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString)**
>
> The toLocaleString() method of Number values returns a string with a language-sensitive representation of this number. In implementations with Intl.NumberFormat API support, this method simply calls Intl.NumberFormat.

> **[Intl.NumberFormat - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)**
>
> The Intl.NumberFormat object enables language-sensitive number formatting.

Final note: If you need a less common locale, it is possible that your implementation of Node.js or browser might not have automatically loaded the right library. Never hit that myself as yet but it is possible and there are ways to load additional locales.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 January 2024 15:32 UTC](https://discourse.nodered.org/t/formatting-numbers-the-right-way/83928/2 "2024-01-20T15:32:46Z")

</div>

This topic was automatically closed after 30 days. New replies are no longer allowed.
