# How do I round a JavaScript number to 2 decimal places?

**URL:** https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571
**Category:** General
**Tags:** node-red-dashboard, function-node
**Created:** [1 July 2022 06:19 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571 "2022-07-01T06:19:15Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![ravi1987](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@ravi1987](https://discourse.nodered.org/u/ravi1987)
#### Post date: [1 July 2022 06:19 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/1 "2022-07-01T06:19:15Z")

</div>

36.99300003051758 I'm receiving  
this kind of value how can round of this

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 July 2022 06:39 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/2 "2022-07-01T06:39:01Z")

</div>

I assume you are using JavaScript, if so this will round up or down to the nearest INTEGER.

> **[JavaScript Math round() Method](https://www.w3schools.com/jsref/jsref_round.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

So in the particular case you posted, the result is 37. Is that what you wanted?  
 ![round](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d738bc7f4552da583708b6bcd3f21a5217bec85.png)

---

<div class="post-metadata">

### Author: ![ravi1987](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@ravi1987](https://discourse.nodered.org/u/ravi1987)
#### Post date: [1 July 2022 06:42 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/3 "2022-07-01T06:42:25Z")

</div>

thank you round of working suppose i want to see only 3 digits means 39.9 what I do

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 July 2022 06:48 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/4 "2022-07-01T06:48:46Z")

</div>

Try this in a 'function' node.

`msg.payload = Math.round((msg.payload) * 100) / 100;`

Gives 36.99 from your posted value.

![round](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/5/a5411e4ee800c66f83b3e30103328d718671ae08.png)

Here's a link to some examples.

> **[Round a Number to 2 Decimal Places in JavaScript](https://www.delftstack.com/howto/javascript/javascript-round-to-2-decimal-places/)**
>
> This tutorial teaches how to round a number to at most \`2\` decimal places in JavaScript?

---

<div class="post-metadata">

### Author: ![ravi1987](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@ravi1987](https://discourse.nodered.org/u/ravi1987)
#### Post date: [1 July 2022 06:54 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/5 "2022-07-01T06:54:00Z")

</div>

thanks lot its working fine

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [1 July 2022 06:58 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/6 "2022-07-01T06:58:45Z")

</div>

Can I suggest you edit the title of your post to something like...

_How to round a JavaScript number to 2 decimal places._

As 'Function node opto 22' would mean nothing to people searching the forum.

---

<div class="post-metadata">

### Author: ![carlosridg](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@carlosridg](https://discourse.nodered.org/u/carlosridg)
#### Post date: [12 August 2022 06:37 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/7 "2022-08-12T06:37:59Z")

</div>

> [@dynamicdave](#):
>
> Math.round((msg.payload) \* 100) / 100;

It seems like Math.round() is a better solution, but it is not! In some cases it will NOT round correctly. Also, toFixed() will NOT round correctly in some cases.

To correct the rounding problem with the previous Math.round() and toFixed(), you can define a custom [JavaScript round](http://net-informations.com/js/progs/round.htm) function that performs a "nearly equal" test to determine whether a fractional value is sufficiently close to a midpoint value to be subject to midpoint rounding. The following function return the value of the given number rounded to the nearest integer accurately.

```auto
Number.prototype.roundTo = function(decimal) {
  return +(Math.round(this + "e+" + decimal) + "e-" + decimal);
}

var num = 9.7654;
console.log( num.roundTo(2)); //output 9.77

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 August 2022 07:03 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/8 "2022-08-12T07:03:18Z")

</div>

Can you give an example of where they give a false value?

```auto
var num = 9.7654;
msg.payload = num.toFixed(2)
return msg;

```

returns 9.77 as expected.

---

<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: [12 August 2022 08:46 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/9 "2022-08-12T08:46:34Z")

</div>

Indeed, I'd be interested too:

```auto
let n = 1.044444444444444444444444444444444444444444444444444444444445

Number.prototype.roundTo = function (decimal) {
    return +(Math.round(this + "e+" + decimal) + "e-" + decimal);
}

return {payload: 
    [n.toFixed(2), Math.round(n * 100) / 100, n.roundTo(2)]
}

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc547310e5cea55f66ae07eec1d577b849191bff.png)

PS: Adding to builtin JS object prototypes is a definite antipattern and should be avoided. It can lead to some virtually impossible to track bugs.

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [12 August 2022 10:52 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/10 "2022-08-12T10:52:09Z")

</div>

When they fail (without touching the exponential)

```auto
let addFailures = 0, multFailures = 0
let n = 1000000000000.1;
let a,b

for (let index = 0; index < 10000; index++) {
    n += 1000000000.01
    a = parseFloat(n.toFixed(2))
    b = Math.round(n * 100) / 100
   
    if(a !== b){
        addFailures ++;
        node.send({
            payload:{
                'operation':'add',
                'index':index,
                'number':n,
                'floatFromFixed':a,
                'Math.Round':b
            }
        })
    }    
}

n = 1.99999;

for (let index = 0; index < 10000; index++) {
    n *= 1.003
    a = parseFloat(n.toFixed(2))
    b = Math.round(n * 100) / 100   
    if (a !== b) {
        multFailures++;
        node.send({
            payload: {
                'operation': 'mult',
                'index': index,
                'number': n,
                'floatFromFixed': a,
                'Math.Round': b
            }
        })
    }
}

return {payload:'add: '+addFailures+ ' mult: '+multFailures}

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [12 August 2022 11:19 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/11 "2022-08-12T11:19:05Z")

</div>

Arguably that is not due to those functions failing, it is due to the basic arithmetic failing when working at the limit of the number of bits available for the calculations. Consider this for example (taking one of the failures found)

```auto
msg.payload = 10292000000091.135
msg.payload = msg.payload + 0.001
return msg;

```

which returns 10292000000091.137

---

<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: [12 August 2022 11:21 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/12 "2022-08-12T11:21:12Z")

</div>

Ah yes, I'd forgotten about that.

In general and for most cases not really an issue. If you really need precision I'd normally use something like [royNiladri/js-big-decimal: Work with large numbers on the client side with high precision. (github.com)](https://github.com/royNiladri/js-big-decimal) - or indeed avoid JS all together and use Python, R or even Fortran(!) which have more mature designs for high-precision number handling.

And as always, only ever round when about to display.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [12 August 2022 11:31 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/13 "2022-08-12T11:31:00Z")

</div>

> [@Colin](#):
>
> Can you give an example of where they give a false value?

```auto
// where Math.round() fails
var num = 1511000000005.2148; // this should round to 1511000000005.21
console.log(num.toFixed(2)); // outputs 1511000000005.21 ✅
console.log(num.roundTo(2)); // outputs 1511000000005.21 ✅
console.log(Math.round(num * 100) / 100); // outputs 1511000000005.22 ❌

// where toFixed() fails
num = 330349199868.985 // this should round to 330349199868.99
console.log(num.toFixed(2)); // outputs 330349199868.98 ❌
console.log(num.roundTo(2)); // outputs 330349199868.99 ✅
console.log(Math.round(num * 100) / 100); // 330349199868.98 ✅

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03de391713226f8d7ba9975161f85a4cdb4ef628.png)

---

<div class="post-metadata">

### Author: ![eksij](https://avatars.discourse-cdn.com/v4/letter/e/9de053/32.png) [@eksij](https://discourse.nodered.org/u/eksij)
#### Post date: [13 August 2022 05:07 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/14 "2022-08-13T05:07:41Z")

</div>

In JSONata  
$formatNumber(12.225, '0.00')  
Can get '12.23'

$number($formatNumber(12.225, '0.00'))  
Can get 12.23

In Function  
var num = 12.225;  
num.toFixed(2);  
Can get '12.22'

---

<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: [13 August 2022 10:02 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/15 "2022-08-13T10:02:54Z")

</div>

> [@Steve-Mcl](#):
>
> `var num = 1511000000005.2148; // this should round to 1511000000005.21`

I am sure that I was taught at school that should be `1511000000005.215` and therefore `1511000000005.22`? Of course, that was over 44 years ago so maybe the accepted wisdom has changed? 😃

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 August 2022 10:24 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/16 "2022-08-13T10:24:24Z")

</div>

`n.2148` is less than `n.2150` so should round down to `n.21`. `n.2150` and above should round to `n.22`.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [13 August 2022 12:12 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/17 "2022-08-13T12:12:42Z")

</div>

I think it depends on weither you round each digit by it self, or all at the same time, i.e  
do you round 5.2148 to two decimal points by rounding  
5.214`8` which would get 5.215 and  
5.21`5` which would become 5.22  
or do you round 5.2148 by rounding the '48' so  
5.21`48` would be rounded down so you get 5.21

---

<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: [13 August 2022 12:45 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/18 "2022-08-13T12:45:44Z")

</div>

Well, we are doubtless well off-topic now 😃

But I vaguely remember rounding right to left.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [13 August 2022 12:45 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/19 "2022-08-13T12:45:45Z")

</div>

Julian rounding is always done by the digit to the right of the one you are rounding. If it is between 0 and 4 you round down if it is five or above you round up. That's all the rules there are.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [13 August 2022 12:46 UTC](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571/20 "2022-08-13T12:46:06Z")

</div>

> [@zenofmud](#):
>
> I think it depends on weither you round each digit by it self, or all at the same time,

It should be done in one go. The idea of rounding is to find the value with the appropriate decimal places that is nearest to the actual value. 5.21 is nearer to 5.2148 (0.0048 away) than is 5.22 (0.0052 away), so 5.21 is the correct rounded value.

[Next page](https://discourse.nodered.org/t/how-do-i-round-a-javascript-number-to-2-decimal-places/64571.md?page=2)
