How do I do math with text from a screen?

I’m trying to verify that the interest calculation on my loan page is correct. I used WebUI.getText to grab the “Loan Amount” and the “Interest Rate” from the screen. But when I try to multiply them together, Katalon gives me a huge error!

It says something like No signature of method: java.lang.String.multiply(). I realize now that the computer thinks “$1,000.00” and “5.5%” are just words, not numbers. I tried to just use as Integer like I saw in a forum post, but that crashed too because of the dollar sign and the comma. Do I have to manually delete the symbols every time the test runs? I’m really stuck on how to turn these screen labels into actual numbers so I can check if the math is right.

2 Likes

This is a classic “Data Sanitization” challenge. In automation, the UI is for humans (filled with symbols like $, %, and ,), but the logic layer requires “clean” numeric types.

To solve this, we use Regular Expressions (Regex) to strip away non-numeric characters and then Type Casting to convert the remaining string into a BigDecimal or Double. Using BigDecimal is the industry standard for financial calculations because it avoids the rounding errors found in floating-point math.

The Solution: String Sanitization

You need to perform three steps:

  1. Extract: Get the text from the UI.

  2. Clean: Remove anything that isn’t a digit or a decimal point.

  3. Convert: Parse the clean string into a number.

Custom Keyword Helper

Here is a robust utility keyword that handles currency, percentages, and commas automatically, returning a clean number you can use for math.

Groovy

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import java.text.DecimalFormat

class MathHelper {

    /**
     * Extracts text from an element and converts it to a clean number
     * Works for "$1,250.50", "5.5%", etc.
     */
    @Keyword
    def parseCurrencyToNumber(TestObject to) {
        String rawText = WebUI.getText(to)
        
        // Use Regex to keep only digits and the decimal point
        // [^0-9.] means "anything that is NOT a number or a dot"
        String cleanText = rawText.replaceAll("[^0-9.]", "")
        
        // Convert to BigDecimal for high precision (ideal for interest/money)
        return cleanText.toBigDecimal()
    }
}

How to use it in your Script:

Now your test script logic looks clean and professional:

Groovy

// 1. Get the numbers using the keyword
def loanAmount = CustomKeywords.'MathHelper.parseCurrencyToNumber'(findTestObject('Object Repository/txt_LoanAmount'))
def interestRate = CustomKeywords.'MathHelper.parseCurrencyToNumber'(findTestObject('Object Repository/txt_InterestRate'))

// 2. Perform the calculation (e.g., Interest = Principal * Rate / 100)
def calculatedInterest = (loanAmount * interestRate) / 100

// 3. Compare with the screen value
def displayedInterest = CustomKeywords.'MathHelper.parseCurrencyToNumber'(findTestObject('Object Repository/txt_TotalInterest'))

assert calculatedInterest == displayedInterest

Are you planning to compare these results against a database value or just verify the UI’s internal math?

1 Like

hi @cdietrich

strip formatting with replaceAll before parsing, and use BigDecimal instead of Double to avoid precision issues

String loanText = WebUI.getText(findTestObject('...')).replaceAll('[^0-9.]', '')
String rateText = WebUI.getText(findTestObject('...')).replaceAll('[^0-9.]', '')

BigDecimal loanAmount = new BigDecimal(loanText)
BigDecimal interestRate = new BigDecimal(rateText).divide(100) // skip if already a decimal
BigDecimal expectedInterest = loanAmount * interestRate

the [^0-9.] regex removes currency symbols, commas, and percent signs in one shot

The solution is: you need to clean the string and convert it to a numeric type (like BigDecimal) before doing math.
You don’t need to manually edit anything each run—just sanitize it in code.