The cleanAndRoundNumberWithPadding extension cleans a numeric string by removing characters other than digits and decimal points, converts the cleaned value to a number, and rounds it to the requested number of decimal places.
This is useful when a journey reads formatted numeric values from the page, such as 1,469.25456, and needs to compare the value after rounding against an expected number.
Parameters:
-
valuerequired, the input string to clean before rounding. The source removes any character that is not a digit or decimal point using/[^0-9.]/g; -
roundingLimitrequired, the number of decimal places to round to. This input is converted withNumber(roundingLimit).
Note: The extension removes commas, currency symbols, spaces, letters, plus signs, minus signs, and other non-numeric characters before parsing. Because the current source returns a JavaScript number, trailing zero padding is not preserved. For example, a result rounded to two decimals may return 1469.2 rather than 1469.20.
How to apply this to your journey
Use the extension in a journey by calling cleanAndRoundNumberWithPadding with the execute command. Pass each value to the matching extension input using as value and as roundingLimit.
Note: The source performs a direct JavaScript calculation and returns the rounded result immediately. Use string or number assertions according to how the returned Virtuoso variable is consumed in your journey.
execute "cleanAndRoundNumberWithPadding" using "1,469.25456" as value and "1" as roundingLimit returning $cleanedValue\nassert $cleanedValue equals "1469.2"execute "cleanAndRoundNumberWithPadding" using "$1,469.25456" as value and "2" as roundingLimit returning $cleanedValue\nassert $cleanedValue equals "1469.25"You can also pass values stored in variables, which is useful when the number is captured from the application during the journey.
execute "cleanAndRoundNumberWithPadding" using "$value" as value and "$roundingLimit" as roundingLimit returning $cleanedValuestore value "1,469.25456" in $value\nstore value "1" in $roundingLimit\nexecute "cleanAndRoundNumberWithPadding" using "$value" as value and "$roundingLimit" as roundingLimit returning $cleanedValue\nassert $cleanedValue equals "1469.2"Example output:
1469.2This extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension is designed for simple decimal-style numeric strings only. It removes everything except digits and decimal points before calling parseFloat, so negative numbers, plus signs, exponent notation, percentages, units, parentheses used for accounting negatives, and locale-specific decimal formats such as 1.469,25 are not handled correctly. Multiple decimal points can also produce partial or unexpected parsing results. The rounding logic uses JavaScript Number, Math.round, and exponential notation, so ordinary binary floating-point precision behavior still applies for edge cases and very large or high-precision values. Despite the extension name, the current source returns a number rather than a fixed-decimal string, so it does not preserve trailing zero padding. Because Virtuoso language extensions execute JavaScript as a test step and cross-browser/device executions can use different browser and platform configurations, validate important numeric assertions in the same browser/device configurations used by your plan, especially when the value is read from localized UI text before being passed into this extension.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 29/04/2025
Resources:
This extension does not require any external resource.
// Last updated: 29/04/2025, 06:37:19 UTC
function cleanInput(input) {
return input.replace(/[^0-9.]/g, '');
}
function rounding(value, decimals) {
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}
let cleanedValue = cleanInput(value);
return rounding(parseFloat(cleanedValue), Number(roundingLimit));
Comments
0 comments
Please sign in to leave a comment.