I have a string in my payload that looks like this:
totalPaid: "268.00"
I need to convert it to a number, and remove the dot (while keeping the two decimals)
Like this:
totalPaid: 26800
Another example:
totalPaid: "350.80"
->
totalPaid: 35080
Would anyone know how to do this?
in a function node...
msg.payload.totalPaid = parseFloat(msg.payload.totalPaid) * 100;
return msg;
Proof...
1 Like
Thanks!
It works fine for some numbers but others come out like this:
"151.20"
->
15119.999999999998
"37.80"
->
3779.9999999999995
Yes, some numbers cannot be represented accurately (this is a floating point thing)
One workaround is to round it...
Tried that now, but it is returned as a string?
This is the input:
netPrice: "151.20"
This is the code:
msg.payload.netPrice = Math.round(parseFloat(msg.payload.netPrice) * 100);
return msg;
And this is the output (should be 15120 without quotes):
netPrice: "151.20"
The code will return a number - you must have some mistake.
Did you deploy? Are you running new changes?
Your code actually works perfectly outside of my subflow (below). This is what I'm trying to do.
I am using this:
https://flows.nodered.org/flow/43501a1b424434de0ffb
To iterate over an array line_items
where there is a key named total
inside each object.
For each iteration, I am using a change node to change total
into netPrice
.
This works perfectly.
Then I need to convert that value (netPrice
) into the forementioned format.
Tried placing the function node here, but that did not work:
Placing it before or after my subflow doesn't work either without specifying which object.
There's no need for loops. Use the split node, do your changes, use a join node to put it all back together?
1 Like
How do I achieve the same thing with the split node? I found a solution that works really well for my use case but the number formatting is the only thing missing
Share your flow and some sample data & I'll show you.
To get sample data add a debug node to where the data comes from & use the "copy value" that appears under your mouse pointer when you over hover a debug message property in the sidebar.
It looks like the split node will only split msg.payload
but I need to split msg.payload.line_items
How do I configure it to do that?
add a change node to move or set msg.payload
to the value of msg.payload.line_items
BEFORE the split.
That worked, but now the rest of the payload is gone...
Noted.
I understand that it's a lot easier to help if I share my flow, but in this case I think that will be difficult because the data I'm working with is pulled from an API that only I have access to.
The only thing remaining that I need help with now is to combine two messages into one.
So I have two messages that each have an array that is called the same (line_items
).
I need to combine them so that I get one message, with an array called line_items
, that has both of the objects inside it (or more, if one of the message's array contains multiple objects).
I have tried using the join node i manual mode with different type of settings, but the closest I can get is this, where the arrays are placed inside a new array:
But I need the object to be in the array directly, if that makes sense?
Like this (photoshopped):
1st line_items
:
{"line_items":[{"description":"T-shirt","netPrice":15920,"vat":3980,"account":3000,"vatType":"HIGH"}]}
2nd line_items
:
{"line_items":[{"description":"Shipping","netPrice":6320,"vat":1580,"account":3020,"vatType":"HIGH"}]}
That doesn't look like source data since it has "netPrice":15920
also, I don't see your flow (can you share what you currently have)
I'm sorry but I odn't think I can provide the info you ask for.
Does anyone else maybe know how I can merge these two into one?
Why not? Is it sensitive? Or do you not know how?