# Rounding floats

**URL:** <https://discourse.nodered.org/t/rounding-floats/53369>\
**Category:** General\
**Created:** [8 November 2021 11:07 UTC](https://discourse.nodered.org/t/rounding-floats/53369 "2021-11-08T11:07:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [8 November 2021 11:07 UTC](https://discourse.nodered.org/t/rounding-floats/53369/1 "2021-11-08T11:07:00Z")

</div>

Hi, guys. Can anyone tell me what is the best way to ROUND a float with 6 decimals to for instance a float with 2 decimals. ".Precision()", has the disadvantage that it includes the number of digits before the comma.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 November 2021 11:08 UTC](https://discourse.nodered.org/t/rounding-floats/53369/2 "2021-11-08T11:08:56Z")

</div>

Try  
Math.round()

> **[Math.round() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round)**
>
> The Math.round() function returns the value of a number
> rounded to the nearest integer.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [8 November 2021 11:11 UTC](https://discourse.nodered.org/t/rounding-floats/53369/3 "2021-11-08T11:11:31Z")

</div>

@E1cid , that will give me an integer.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 November 2021 11:16 UTC](https://discourse.nodered.org/t/rounding-floats/53369/4 "2021-11-08T11:16:16Z")

</div>

sorry you are correct  
try

```auto
Number(msg.payload.toFixed(2))

```

or

```auto
Math.round(float*100)/100

```

---

<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:** [8 November 2021 11:27 UTC](https://discourse.nodered.org/t/rounding-floats/53369/5 "2021-11-08T11:27:14Z")

</div>

Why are you wanting to do this? If you are sending it to a dashboard node, for example, most of them allow you to do this at display time by using something like this in the Value Format field  
`{{msg.payload | number: 2}}`  
Generally numbers should not be rounded until they get to the display, it is not good to throw away accuracy before it is necessary.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [8 November 2021 11:34 UTC](https://discourse.nodered.org/t/rounding-floats/53369/6 "2021-11-08T11:34:11Z")

</div>

The complex way, but does exactly what you wants.

```auto
var myvalue = 12.345678
var myvaluestr = myvalue.toString()
var index = myvaluestr.indexOf(".")
msg.payload= myvalue.toPrecision(index+2)

```

Gives 12.35

---

<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:** [8 November 2021 11:46 UTC](https://discourse.nodered.org/t/rounding-floats/53369/7 "2021-11-08T11:46:07Z")

</div>

What is wrong with using `ToFixed` if it is necessary to do it in javascript? It should give exactly the same result without the additional code.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [8 November 2021 11:55 UTC](https://discourse.nodered.org/t/rounding-floats/53369/8 "2021-11-08T11:55:41Z")

</div>

@Colin and @E1cid , you are completely right the both of you. I assumed that "Number" was truncating the float.Thanks for the answers.  
To @edje11 , indeed your solution is complex and does the same, also thanks for your answer.

---

<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:** [22 November 2021 11:55 UTC](https://discourse.nodered.org/t/rounding-floats/53369/9 "2021-11-22T11:55:42Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
