Convert a given $ value to a specific format - working, but must be an easier way

Situation:

I have a monetary value that’s consistently the following format… [xxxx][xx] → the last two digits are always the “cents” and anything before them are the dollar amt.

example:
value: 10 = $0.10
value: 100 = $1.00
value: 1300 = $13.00
value: 19912 = 199.12
value: 34920112 = $349,201.12

The goal is a formula which converts the given value into the monetary output.

Here’s what I have that works:

With the output:
image

Question - surely they’s a simpler way to accomplish this other than the narly substring flow I have now?

${{substring(1.value; 0; length(1.value) - 2)}}.{{substring(1.value; length(1.value) - 2; length(1.value))}}

Here’s the two test modules for a closer look:

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 1,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "variables": [
                            {
                                "name": "value",
                                "value": "197753"
                            }
                        ],
                        "scope": "roundtrip"
                    },
                    "metadata": {
                        "designer": {
                            "x": 266,
                            "y": -16,
                            "name": "inputs"
                        },
                        "restore": {
                            "expect": {
                                "variables": {
                                    "items": [
                                        null
                                    ]
                                },
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "variables",
                                "type": "array",
                                "label": "Variables",
                                "spec": [
                                    {
                                        "name": "name",
                                        "label": "Variable name",
                                        "type": "text",
                                        "required": true
                                    },
                                    {
                                        "name": "value",
                                        "label": "Variable value",
                                        "type": "any"
                                    }
                                ]
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            }
                        ],
                        "interface": [
                            {
                                "name": "value",
                                "label": "value",
                                "type": "any"
                            }
                        ]
                    }
                },
                {
                    "id": 2,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "variables": [
                            {
                                "name": "get last 2 numbers",
                                "value": "{{substring(1.value; length(1.value) - 2; length(1.value))}}"
                            },
                            {
                                "name": "all numbers before last 2",
                                "value": "{{substring(1.value; 0; length(1.value) - 2)}}"
                            },
                            {
                                "name": "combined",
                                "value": "${{substring(1.value; 0; length(1.value) - 2)}}.{{substring(1.value; length(1.value) - 2; length(1.value))}}"
                            },
                            {
                                "name": "raw value",
                                "value": "{{1.value}}"
                            }
                        ],
                        "scope": "roundtrip"
                    },
                    "metadata": {
                        "designer": {
                            "x": 575,
                            "y": -18,
                            "name": "value of an amt."
                        },
                        "restore": {
                            "expect": {
                                "variables": {
                                    "items": [
                                        null,
                                        null,
                                        null,
                                        null
                                    ]
                                },
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "variables",
                                "type": "array",
                                "label": "Variables",
                                "spec": [
                                    {
                                        "name": "name",
                                        "label": "Variable name",
                                        "type": "text",
                                        "required": true
                                    },
                                    {
                                        "name": "value",
                                        "label": "Variable value",
                                        "type": "any"
                                    }
                                ]
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            }
                        ],
                        "interface": [
                            {
                                "name": "get last 2 numbers",
                                "label": "get last 2 numbers",
                                "type": "any"
                            },
                            {
                                "name": "all numbers before last 2",
                                "label": "all numbers before last 2",
                                "type": "any"
                            },
                            {
                                "name": "combined",
                                "label": "combined",
                                "type": "any"
                            },
                            {
                                "name": "raw value",
                                "label": "raw value",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

You can use the built-in function formatNumber

e.g.:

${{formatNumber(31.value / 100; 2; "."; ",")}}

Screenshot_2024-03-12_120341

Output

Screenshot_2024-03-12_120346

For more information, see Math functions

2 Likes

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

  1. Copy the 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 to paste in the canvas.

  3. Click on each imported module and save it. You may need to remap some variables.

Modules JSON Export

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 33,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "currency",
                        "scope": "roundtrip",
                        "value": "${{formatNumber(31.value / 100; 2; \".\"; \",\")}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 587,
                            "y": -1334
                        },
                        "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": "currency",
                                "label": "currency",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}
2 Likes

Bro, you always have the best answers hahah THANK YOU

2 Likes