How do I perform arithmetic operations on numbers with special characters like currency symbol or comma
This example demonstrates performing an arithmetical operation for two or more values using the "storeEval" command.
In the below solution, you store the prices of the product in two variables and remove the special characters using the "storeEval" command. Now add both the prices using the "sum" command. Later append currency symbol "$" to the final price.
Solution:
# | Command | Target | Value |
---|---|---|---|
Store the price of the product1 |
|||
1 | store | $119 |
ProductPrice1 |
Store the price of the product2 | |||
2 | store |
$192 | ProductPrice2 |
Remove '$' from the Product Price1 | |||
3 | storeEval |
function dollarRemoval() { var price="${ProductPrice1}".replace(/\$/g,''); return price; } dollarRemoval(); |
ProductPrice1 |
Remove '$' from the ProductPrice2 | |||
4 | storeEval |
function dollarRemoval() { var price="${ProductPrice2}".replace(/\$/g,''); return price; } dollarRemoval(); |
ProductPrice2 |
Add the ProductPrice1 and ProductPrice2 by using the sum command | |||
5 | sum |
${ProductPrice1},${ProductPrice2} |
CartPrice |
Store the values to be concatenated | |||
6 | concatenate |
$,${CartPrice} |
TotalPrice |
Tips, Tricks, Gotchas & Best Practices:
- The list of numeric values to be added should be separated by a comma(,)
- As a best practice, it is always advisable to define the variable names without any spaces and if you want to differentiate the words, use underscores.
- The variable name used in the 'Value' field of the Store command should not contain the following special characters which are not allowed: >, <, ==, !=, >=, <=, +, -, *, /, %, (, )