# How do I do math with text from a screen?

**URL:** <https://forum.katalon.com/t/how-do-i-do-math-with-text-from-a-screen/190372>\
**Category:** Web Testing\
**Created:** [April 25, 2026, 11:39am UTC](https://forum.katalon.com/t/how-do-i-do-math-with-text-from-a-screen/190372 "2026-04-25T11:39:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdietrich](https://avatars.discourse-cdn.com/v4/letter/c/d2c977/32.png) [@cdietrich](https://forum.katalon.com/u/cdietrich)\
**Post date:** [April 25, 2026, 11:39am UTC](https://forum.katalon.com/t/how-do-i-do-math-with-text-from-a-screen/190372/1 "2026-04-25T11:39:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![qurzunta.kaab](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/qurzunta.kaab/32/31470_2.png) [@qurzunta.kaab](https://forum.katalon.com/u/qurzunta.kaab)\
**Post date:** [April 25, 2026, 11:45am UTC](https://forum.katalon.com/t/how-do-i-do-math-with-text-from-a-screen/190372/2 "2026-04-25T11:45:48Z")

</div>

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

```auto
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

```auto
// 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?

---

<div class="post-metadata">

**Author:** ![depapp](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/depapp/32/29178_2.png) [@depapp](https://forum.katalon.com/u/depapp)\
**Post date:** [April 25, 2026, 11:46am UTC](https://forum.katalon.com/t/how-do-i-do-math-with-text-from-a-screen/190372/3 "2026-04-25T11:46:31Z")

</div>

hi @cdietrich

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

```groovy
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

---

<div class="post-metadata">

**Author:** ![arvind.choudhary](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arvind.choudhary/32/33165_2.png) [@arvind.choudhary](https://forum.katalon.com/u/arvind.choudhary)\
**Post date:** [April 25, 2026, 4:00pm UTC](https://forum.katalon.com/t/how-do-i-do-math-with-text-from-a-screen/190372/4 "2026-04-25T16:00:14Z")

</div>

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.
