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) =>{
    msg.payload[v[0]] = Math.round(v[1]);
})
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",
        "func": "Bez_T = msg.payload.Bez - msg.payload.Bez_T;\nBez_T = parseFloat(Bez_T.toFixed(2));\nBez_M = msg.payload.Bez - msg.payload.Bez_M;\nBez_M = parseFloat(Bez_M.toFixed(2));\nBez_J = msg.payload.Bez - msg.payload.Bez_J;\nBez_J = parseFloat(Bez_J.toFixed(2));\nEin_T = msg.payload.Ein - msg.payload.Ein_T;\nEin_T = parseFloat(Ein_T.toFixed(2));\nEin_M = msg.payload.Ein - msg.payload.Ein_M;\nEin_M = parseFloat(Ein_M.toFixed(2));\nEin_J = msg.payload.Ein - msg.payload.Ein_J;\nEin_J = parseFloat(Ein_J.toFixed(2));\nPV_T = msg.payload.PV - msg.payload.PV_T;\nPV_T = parseFloat(PV_T.toFixed(2));\nPV1_T = parseFloat((PV_T/21.23).toFixed(2));\nPV_M = msg.payload.PV - msg.payload.PV_M;\nPV_M = parseFloat(PV_M.toFixed(2));\nPV1_M = parseFloat((PV_M/21.23).toFixed(2));\nPV_J = msg.payload.PV - msg.payload.PV_J;\nPV_J = parseFloat(PV_J.toFixed(2));\nPV1_J = parseFloat((PV_J/21.23).toFixed(2));\nBel_T = msg.payload.Bel - msg.payload.Bel_T;\nBel_T = parseFloat(Bel_T.toFixed(2));\nEnt_T = msg.payload.Ent - msg.payload.Ent_T;\nEnt_T = parseFloat(Ent_T.toFixed(2));\nBel_M = msg.payload.Bel - msg.payload.Bel_M;\nBel_M = parseFloat(Bel_M.toFixed(2));\nEnt_M = msg.payload.Ent - msg.payload.Ent_M;\nEnt_M = parseFloat(Ent_M.toFixed(2));\nBel_J = msg.payload.Bel - msg.payload.Bel_J;\nBel_J = parseFloat(Bel_J.toFixed(2));\nEnt_J = msg.payload.Ent - msg.payload.Ent_J;\nEnt_J = parseFloat(Ent_J.toFixed(2));\nTw = (Ent_T/Bel_T )*100;\nTw = parseFloat(Tw.toFixed(2));\nMw = (Ent_M/Bel_M)*100;\nMw = parseFloat(Mw.toFixed(2));\nJw = (Ent_J/Bel_J)*100;\nJw = parseFloat(Jw.toFixed(2));\ngoe_T = msg.payload.goe - msg.payload.goe_T;\ngoe_T = parseFloat(goe_T.toFixed(2));\ngoe_M = msg.payload.goe - msg.payload.goe_M;\ngoe_M = parseFloat(goe_M.toFixed(2));\ngoe_J = msg.payload.goe - msg.payload.goe_J;\ngoe_J = parseFloat(goe_J.toFixed(2));\nopenwb_T = msg.payload.openwb - msg.payload.openwb_T;\nopenwb_T = parseFloat(openwb_T.toFixed(2));\nopenwb_M = msg.payload.openwb - msg.payload.openwb_M;\nopenwb_M = parseFloat(openwb_M.toFixed(2));\nopenwb_J = msg.payload.openwb - msg.payload.openwb_J;\nopenwb_J = parseFloat(openwb_J.toFixed(2));\nV = Bez_T - Ein_T + PV_T + Ent_T - Bel_T;\nH = V - goe_T - openwb_T;\nV = parseFloat(V.toFixed(2));\nH = parseFloat(H.toFixed(2));\nDV = PV_T - Bel_T - Ein_T;\nDV = parseFloat(DV.toFixed(2));\nEVo = PV_T - Bel_T + Ent_T - Ein_T;\nEVo = parseFloat(EVo.toFixed(2));\nEVr = PV_T - Ein_T;\nEVr = parseFloat(EVr.toFixed(2));\nAQ = (EVo/V)*100;\nAQ = parseFloat(AQ.toFixed(2));\nDVQ = (DV/PV_T)*100;\nDVQ = parseFloat(DVQ.toFixed(2));\nEVQ = (EVr/PV_T)*100;\nEVQ = parseFloat(EVQ.toFixed(2));\nV1 = Bez_M - Ein_M + PV_M + Ent_M - Bel_M;\nH1 = V1 - goe_M - openwb_M;\nV1 = parseFloat(V1.toFixed(2));\nH1 = parseFloat(H1.toFixed(2));\nDV1 = PV_M - Bel_M - Ein_M;\nDV1 = parseFloat(DV1.toFixed(2));\nEVo1 = PV_M - Bel_M + Ent_M - Ein_M;\nEVo1 = parseFloat(EVo1.toFixed(2));\nEVr1 = PV_M - Ein_M;\nEVr1 = parseFloat(EVr1.toFixed(2));\nAQ1 = (EVo1/V1)*100;\nAQ1 = parseFloat(AQ1.toFixed(2));\nDVQ1 = (DV1/PV_M)*100;\nDVQ1 = parseFloat(DVQ1.toFixed(2));\nEVQ1 = (EVr1/PV_M)*100;\nEVQ1 = parseFloat(EVQ1.toFixed(2));\nV2 = Bez_J - Ein_J + PV_J + Ent_J - Bel_J;\nH2 = V2 - goe_J - openwb_J;\nV2 = parseFloat(V2.toFixed(2));\nH2 = parseFloat(H2.toFixed(2));\nDV2 = PV_J - Bel_J - Ein_J;\nDV2 = parseFloat(DV2.toFixed(2));\nEVo2 = PV_J - Bel_J + Ent_J - Ein_J;\nEVo2 = parseFloat(EVo2.toFixed(2));\nEVr2 = PV_J - Ein_J;\nEVr2 = parseFloat(EVr2.toFixed(2));\nAQ2 = (EVo2/V2)*100;\nAQ2 = parseFloat(AQ2.toFixed(2));\nDVQ2 = (DV2/PV_J)*100;\nDVQ2 = parseFloat(DVQ2.toFixed(2));\nEVQ2 = (EVr2/PV_J)*100;\nEVQ2 = parseFloat(EVQ2.toFixed(2));\nmsg.payload = {Bez_M,Bez_T,Bez_J,Ein_M,Ein_T,Ein_J,PV_T,PV1_T,PV_M,PV1_M,PV_J,PV1_J,Bel_T,Ent_T,Bel_M,Ent_M,Bel_J,Ent_J,Tw,Mw,Jw,goe_T,goe_M,goe_J,openwb_T,openwb_M,openwb_J,V,H,DV,EVo,EVr,AQ,DVQ,EVQ,V1,H1,DV1,EVo1,EVr1,AQ1,DVQ1,EVQ1,V2,H2,DV2,EVo2,EVr2,AQ2,DVQ2,EVQ2};\nmsg.topic = \"Auswertung\";\nreturn msg;",
        "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)).
:face_with_monocle:

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 :grin:

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.

can you please tell me?
or give me a link?

Why is 1.35 in java = 1.350000000000000088817841970012523233890533447265625 ?

There is a reasonable workaround if you dont care about this: in a function node

Object.entries(msg.payload).forEach((v) =>{
    msg.payload[v[0]] = precisionRound(v[1], 2);
})
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
image

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.