How Do I format a Number to 3 Digits?

All I want to do is set a number “1” to be 001.

or if the number is “99” then "099:

or if its 150 then “150”.

I’ve tried ChatGPT code 50 times none of them work inside of Set Variable" module.

This is called the “padding” of a number.

You can do something like this:

{{replace("000" + 1.number; "/^0+(?=\d{3})/"; emptystring)}}

Screenshot_2024-05-20_120737

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

View Module Export Code

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 92,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "padded_number",
                        "scope": "roundtrip",
                        "value": "{{replace(\"000\" + 91.number; \"/^0+(?=\\d{3})/\"; emptystring)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -872,
                            "y": -1617,
                            "name": "Pad Number"
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "padded_number",
                                "label": "padded_number",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
2 Likes