Convert string to number

Hi
How can I do to convert value and date string to number in n8n?

I’m receive like this in my google sheets: '49850, I wanto to transform to this: 498,50

I’m receive like this in my google sheets: '07/10/2021 12:00:52, I wanto to transform to this: 07/10/2021 12:00:52

Hi @Frank_da_Silva_Costa,

To convert a string to a number you would want to use the parseInt() function, You should be able to use this in an expression so it would be something like {{parseInt($json["myString"])}}

If you wanted the number to be split with a comma it it just a case of adding .toLocaleString() so you would have {{parseInt($json["myString"]).toLocaleString()}} but as this contains a comma it would no longer be considered an int and would be a string.

The example below shows both of these options in a function node so you can get a feel for what they do.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -1010,
        -280
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "myString",
              "value": "123456"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -810,
        -280
      ]
    },
    {
      "parameters": {
        "functionCode": "for (item of items) {\n  item.json.myNewNumber = parseInt(item.json.myString);\n  item.json.myNumberWithComma = parseInt(item.json.myString).toLocaleString();\n}\nreturn items;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -610,
        -280
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

With the second part of your question the 2 dates look to be the same so I am not sure what it is you want to do with them but for date conversions there is a handy Date & Time node which is well documented and can deal with that sort of thing.

3 Likes

Tnk you very munch

Something like this?

{{parseInt($node[“Webhook”].json[“body”][“trans_value”])}}

That would do it if the value existed.

Hi Jon, tnks for you patience with me

Ai already do it, but when append in the sheets still appear with " ’ "

Do you know if is there other way to remove this " ’ " ? and the field come as numer


image