# Round all values of an object (2)

Hi,
is it possible to round all values of an object with "parseFloat" like this:

``````Object.entries(msg.payload).forEach((v) =>{
})
return msg;
``````

but with any decimal places like parseFloat(#.toFixed(2)) ?

Greetings

`Math.round()` rounds to the nearest integer.

There is little point in using toFixed() and then parsing it back to a float, as the result may be a value which is not fixed any more (due to rounding errors). If you want a fixed point value then keep it as a string.

If you require a float then you can do

``````Math.round(v[1]*100)/100
``````

thanks, my function is very long because every value has to parsefloated:

``````[
{
"id": "096d0b59bafc40bb",
"type": "function",
"z": "112d382639b7cc28",
"name": "Werte",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 930,
"y": 2240,
"wires": [
]
}
]
``````

IĀ“m a little bit confused if itĀ“s better to use Math.round(x) or parseFloat(x.toFixed(0)) \// Math.round (x/100)*100 or parseFloat(x.toFixed(2)).

The answer is really neither. You should only round when displaying data because you lose precision.

Why are you bothering to round the values?

ThatĀ“s clear, I meant whether the two functions are 100% identical.

I donĀ“t unterstand why 1.35+1.35+1.35 is not 4.05

I can tell you but it won't really help.

In simple terms, there is no way to map `4.05` to a finite binary floating point number. So let the numbers be what they are until you need to display them - then use angular formatters or a function node & .toFixed (which returns a string). That's what they are for.

Why is 1.35 in java = 1.350000000000000088817841970012523233890533447265625 ?

``````Object.entries(msg.payload).forEach((v) =>{
})
return msg;

function precisionRound(number, precision) {
var factor = Math.pow(10, precision);
var n = precision < 0 ? number : 0.01 / factor + number;
return Math.round( n * factor) / factor;
}
``````

Proof

1 Like

this is very helpfull too: IEEE-754 Floating Point Converter

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