# String to number with decimals (but without separator)

**URL:** https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178
**Category:** General
**Created:** [28 May 2022 10:43 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178 "2022-05-28T10:43:08Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [28 May 2022 10:43 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/1 "2022-05-28T10:43:08Z")

</div>

I have a string in my payload that looks like this:

```auto
totalPaid: "268.00"

```

I need to convert it to a number, and remove the dot (while keeping the two decimals)

Like this:

```auto
totalPaid: 26800

```

Another example:

```auto
totalPaid: "350.80"

```

-\>

```auto
totalPaid: 35080

```

Would anyone know how to do this?

---

<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: [28 May 2022 10:48 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/2 "2022-05-28T10:48:05Z")

</div>

in a function node...

```javascript
msg.payload.totalPaid = parseFloat(msg.payload.totalPaid) * 100;
return msg;

```

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

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [28 May 2022 11:03 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/3 "2022-05-28T11:03:03Z")

</div>

Thanks!

It works fine for some numbers but others come out like this:

"151.20"  
-\>  
15119.999999999998

"37.80"  
-\>  
3779.9999999999995

---

<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: [28 May 2022 11:12 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/4 "2022-05-28T11:12:12Z")

</div>

Yes, some numbers cannot be represented accurately (this is a floating point thing)

One workaround is to round it...

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

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [28 May 2022 11:23 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/5 "2022-05-28T11:23:23Z")

</div>

Tried that now, but it is returned as a string? 😕

This is the input:

```auto
netPrice: "151.20"

```

This is the code:

```auto
msg.payload.netPrice = Math.round(parseFloat(msg.payload.netPrice) * 100);
return msg;

```

And this is the output (should be 15120 without quotes):

```auto
netPrice: "151.20"

```

---

<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: [28 May 2022 11:26 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/6 "2022-05-28T11:26:12Z")

</div>

The code will return a number - you must have some mistake.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/877b555fa0a8148fa3efffb26ce8b97cb0759a93.png)

Did you deploy? Are you running new changes?

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [28 May 2022 11:51 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/7 "2022-05-28T11:51:40Z")

</div>

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](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`.

![change](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58e532d19df3ed356f4d82eb9778f7946b1483f5.jpeg)

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:

![nr](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9550f81ef32559b7579eddafca19d6e5a8d7cc7.jpeg)

Placing it before or after my subflow doesn't work either without specifying which object.

---

<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: [28 May 2022 11:57 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/8 "2022-05-28T11:57:29Z")

</div>

There's no need for loops. Use the split node, do your changes, use a join node to put it all back together?

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [29 May 2022 07:31 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/9 "2022-05-29T07:31:00Z")

</div>

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

---

<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: [29 May 2022 07:35 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/10 "2022-05-29T07:35:26Z")

</div>

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.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [29 May 2022 08:15 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/11 "2022-05-29T08:15:33Z")

</div>

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?

---

<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: [29 May 2022 08:20 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/12 "2022-05-29T08:20:18Z")

</div>

> [@network\_potato](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [29 May 2022 08:46 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/13 "2022-05-29T08:46:57Z")

</div>

That worked, but now the rest of the payload is gone...

---

<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: [29 May 2022 08:51 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/14 "2022-05-29T08:51:43Z")

</div>

2 things.

1. If you dont use the reply button attached to the entry the user you are replying, they don't get a notification  

2. If you don't share more info, it is hard to help.

👇

> [@Steve-Mcl](#):
>
> Share your flow and some sample data & I'll show you.

👆

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [29 May 2022 17:48 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/15 "2022-05-29T17:48:22Z")

</div>

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`).  
 ![122233](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55229baf07c6d5828b3545af2698b930b8889e73.jpeg)

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:

![333](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/879c58535ed36d9676d00a082e35800930683f5b.jpeg)

But I need the object to be in the array directly, if that makes sense?  
Like this (photoshopped):

![mockup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83bbe50524e269724862675498d71aa4c04a7d71.jpeg)

---

<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: [29 May 2022 20:26 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/16 "2022-05-29T20:26:26Z")

</div>

> [@network\_potato](#):
>
> 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.

That's why I said

> [@Steve-Mcl](#):
>
> 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.
> 
> ![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [30 May 2022 06:01 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/17 "2022-05-30T06:01:52Z")

</div>

1st `line_items`:

```auto
{"line_items":[{"description":"T-shirt","netPrice":15920,"vat":3980,"account":3000,"vatType":"HIGH"}]}

```

2nd `line_items`:

```auto
{"line_items":[{"description":"Shipping","netPrice":6320,"vat":1580,"account":3020,"vatType":"HIGH"}]}

```

---

<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: [30 May 2022 06:07 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/18 "2022-05-30T06:07:23Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)
#### Post date: [8 June 2022 12:06 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/19 "2022-06-08T12:06:23Z")

</div>

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? 😕

---

<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: [8 June 2022 12:11 UTC](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178/20 "2022-06-08T12:11:17Z")

</div>

> [@network\_potato](#):
>
> I'm sorry but I odn't think I can provide the info you ask for

Why not? Is it sensitive? Or do you not know how?

[Next page](https://discourse.nodered.org/t/string-to-number-with-decimals-but-without-separator/63178.md?page=2)
