Greater Than > < Smaller Than Does Not Work Correctly

You are comparing strings, not numbers. If you want to compare numbers, convert the string to a number first with parseFloat.

Plus in your case specifically: Replace the comma with a point!

The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the “11” or “02” portion.

To fix this convert the commas to decimal points.

THis works:

    {
      "Name": "compare numbers",
      "CreationDate": "2021-4-2",
      "Commands": [
        {
          "Command": "store",
          "Target": "003,14",
          "Value": "pi"
        },
        {
          "Command": "executeScript_Sandbox",
          "Target": "var a = ${pi}; return a.replace(',', '.');",
          "Value": "pi"
        },
        {
          "Command": "echo",
          "Target": "comma removed: ${pi}",
          "Value": "green"
        },
        {
          "Command": "store",
          "Target": "2.712345",
          "Value": "e"
        },
        {
          "Command": "if_v2",
          "Target": "${pi} > ${e}  ",
          "Value": ""
        },
        {
          "Command": "echo",
          "Target": "this is comparing STRINGS, and \"0\" is higher in the alphabet as \"2\"",
          "Value": "blue"
        },
        {
          "Command": "end",
          "Target": "",
          "Value": ""
        },
        {
          "Command": "if_v2",
          "Target": "parseFloat (${pi}) > parseFloat (${e})  ",
          "Value": ""
        },
        {
          "Command": "echo",
          "Target": "Now we are comparing numbers!",
          "Value": "green"
        },
        {
          "Command": "end",
          "Target": "",
          "Value": ""
        }
      ]
    }
2 Likes