Expression to convert string of different numbers

Any tips on running an expression that may have a field that contains “1”, “12”, “54.99”, etc. and convert that to add numbers or remove decimal if there is a decimal?

So,
if field is “1” it becomes “100”
and if “12” it becomes “1200”
and if “54.99” becomes “5499”
and if “1399.99” becomes 139999
all in the same expression?

Hey @jtas21,

At a guess you are working with currencies and the system you are playing with needs the value to be made up of the lowest currency form.

All you would need to do is x the value by 100, in the expression something like {{variable * 100}}

I will get an example put together in an hour or so for you if you have not worked it out by then.

Quick example below, It has one set node to set 4 values using a mix of decimal and whole numbers. The second set node shows how to use the expressions to make the change you want.

Just for fun I used a mixed of strings and numbers just to show that it doesn’t really matter what type of data you have.

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "valueOne",
              "value": "12"
            },
            {
              "name": "valueTwo",
              "value": "54.99"
            }
          ],
          "number": [
            {
              "name": "valueThree",
              "value": 1.99
            },
            {
              "name": "valueFour",
              "value": 133.99
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "oneChange",
              "value": "={{$json[\"valueOne\"]*100}}"
            },
            {
              "name": "twoChange",
              "value": "={{$json[\"valueTwo\"]*100}}"
            }
          ],
          "number": [
            {
              "name": "threeChange",
              "value": "={{$json[\"valueThree\"]*100}}"
            },
            {
              "name": "fourChange",
              "value": "={{$json[\"valueFour\"]*100}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hopefully this helps :+1:

1 Like

Awesome thanks, @jon!

1 Like