# Unicode to number

**URL:** <https://discourse.nodered.org/t/unicode-to-number/22758>\
**Category:** General\
**Created:** [7 March 2020 14:43 UTC](https://discourse.nodered.org/t/unicode-to-number/22758 "2020-03-07T14:43:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![StianB](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@StianB](https://discourse.nodered.org/u/StianB)\
**Post date:** [7 March 2020 14:43 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/1 "2020-03-07T14:43:58Z")

</div>

Hi,

I am having some issues to convert what appears to be unicode into number. I am running a python script from a exec module where the msg.payload returns a value which seems to be unicode(?)

 ![nodered1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/43a673c28466eb749895a7eb7b335cef56f148dd.png)

Adding a change node returns an error.

 ![nodered2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/287575781fb8c1a9494e473a2a06badacf1cafd8.png)

Any idea how this could be solved with very limited knowledge related to programming..

br  
Stian

---

<div class="post-metadata">

**Author:** ![StianB](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@StianB](https://discourse.nodered.org/u/StianB)\
**Post date:** [7 March 2020 14:45 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/2 "2020-03-07T14:45:34Z")

</div>

Changenode looks like this:

![nodered3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/252a9b632c18f2ea8ef2084fe91d664a3ba5986f.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 March 2020 14:47 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/3 "2020-03-07T14:47:03Z")

</div>

It isn't a unicode issue, you simply have a trailing newline (`\n`). Apply `trim` to the string before conversion.

Trimming input strings is nearly always the best approach anyway.

---

<div class="post-metadata">

**Author:** ![StianB](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@StianB](https://discourse.nodered.org/u/StianB)\
**Post date:** [7 March 2020 14:49 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/4 "2020-03-07T14:49:08Z")

</div>

Hi, is that applied in the change node or a seperate node?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [7 March 2020 14:51 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/5 "2020-03-07T14:51:26Z")

</div>

You can do it in your JSONata. Something like (untested): `$number($trim(payload))` I think.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [9 March 2020 20:30 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/6 "2020-03-09T20:30:06Z")

</div>

Julian is correct that you would need to get rid of the trailing newline character -- but the embedded comma is still going to cause the conversion to fail...

If you know that the payload will only contain a formatted number string, you could use:  
`$replace(payload, /\D/, "").$number()`

This strips out all of the non-digit characters (including newlines) and then tries to convert the remaining digits to a number.

But in general, it would be best to modify the python code to return a valid number (unformatted) without any whitespace or line breaks...

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 March 2020 21:00 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/7 "2020-03-09T21:00:35Z")

</div>

> [@shrickus](#):
>
> the embedded comma is still going to cause the conversion to fail

Ah, your eyesight (or monitor) is obviously better than mine as I completely missed that!

I guess that could be a locale issue though since many European languages swap what we would call "normal" - e.g. "2,163,000.45" becomes "2.163.000,45"

---

<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:** [8 May 2020 21:00 UTC](https://discourse.nodered.org/t/unicode-to-number/22758/8 "2020-05-08T21:00:37Z")

</div>

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