# How to convert String to int in Katalon

**URL:** <https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [November 27, 2017, 6:46pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371 "2017-11-27T18:46:49Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhawna\_Rani](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhawna_rani/32/16104_2.png) [@Bhawna\_Rani](https://forum.katalon.com/u/Bhawna_Rani)\
**Post date:** [November 27, 2017, 6:46pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/1 "2017-11-27T18:46:49Z")

</div>

Hello,

I am running following code and getting compilation error for the lines where I am parsing String to Int.  
List ShiftGrid\_Rows= DriverFactory.webDriver.findElements(By.xpath(’//tbody/tr’))  
for (WebElement ShiftGridRow:ShiftGrid\_Rows)  
{  
String Guest\_to\_Shift\_Ratio= ShiftGridRow.findElement(By.xpath(’/td[2]’)).getText();  
Int Guest\_Ratio= Integer.parseInt(Guest\_to\_Shift\_Ratio)  
println ('Guest to shift Ratio is: '+Guest\_to\_Shift\_Ratio)

```
		String Guest_Count= WebUI.getText(findTestObject('Object Repository/Locators/Events/Shift Grid Panel/Guest_Count_Total'))
		Int Guest_Count_Int= Integer.parseInt(Guest_Count)
		println ('Guest Count is: '+Guest_Count)
		 
		Int Expected_Projected_Shifts= Guest_Ratio/Guest_Count_Int
		
		Mod= Guest_to_Shift_Ratio%Guest_Count
		
		if (Mod!=0) {
			Expected_Projected_Shifts= Expected_Projected_Shifts+1
		}
		else {
			Expected_Projected_Shifts= Expected_Projected_Shifts
		}
		
		String Actual_Projected_Shifts = ShiftGridRow.findElement(By.xpath('/td[3]')).getText()
		Int Projected_Shifts= Integer.parseInt(Actual_Projected_Shifts)
	
		//WebUI.verifyEqual(Actual_Projected_Shifts, Expected_Projected_Shifts)
		Assert.assertEquals(Projected_Shifts, Expected_Projected_Shifts);
	}

```

Because I have to some math operations, I need to convert Guest\_to\_Shift\_Ratio and Guest\_Count in Integers. But I get the following Compilation error.

11-26-2017 08:32:57 PM - [ERROR] - Test Cases/Events/Create Event- Valid Inputs FAILED because (of) org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 65: unable to resolve class Int  
@ line 65, column 8.  
Int Guest\_Ratio= Integer.parseInt(Guest\_to\_Shift\_Ratio)  
^

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 69: unable to resolve class Int  
@ line 69, column 8.  
Int Guest\_Count\_Int= Integer.parseInt(Guest\_Count)  
^

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 72: unable to resolve class Int  
@ line 72, column 8.  
Int Expected\_Projected\_Shifts= Guest\_Ratio/Guest\_Count\_Int  
^

file:/C:/Users/b.rani/git/schedule-xpress-katalon/Scripts/Common/Test%20cases%20to%20be%20reused/Shift%20Grid%20Panel/Script1506712948187.groovy: 86: unable to resolve class Int  
@ line 86, column 8.  
Int Projected\_Shifts= Integer.parseInt(Actual\_Projected\_Shifts)  
^

4 errors

---

<div class="post-metadata">

**Author:** ![Bhawna\_Rani](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhawna_rani/32/16104_2.png) [@Bhawna\_Rani](https://forum.katalon.com/u/Bhawna_Rani)\
**Post date:** [December 3, 2017, 6:03am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/2 "2017-12-03T06:03:20Z")

</div>

I have solved the problem. Thanks

---

<div class="post-metadata">

**Author:** ![Anson](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Anson](https://forum.katalon.com/u/Anson)\
**Post date:** [December 26, 2017, 7:22am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/3 "2017-12-26T07:22:24Z")

</div>

Hi Rani,

able to advise how you do it?

---

<div class="post-metadata">

**Author:** ![manisha1](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@manisha1](https://forum.katalon.com/u/manisha1)\
**Post date:** [January 23, 2018, 11:32am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/4 "2018-01-23T11:32:35Z")

</div>

How you solve the problem, can you please advise

---

<div class="post-metadata">

**Author:** ![Bhawna\_Rani](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhawna_rani/32/16104_2.png) [@Bhawna\_Rani](https://forum.katalon.com/u/Bhawna_Rani)\
**Post date:** [January 29, 2018, 5:25pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/5 "2018-01-29T17:25:37Z")

</div>

Use the Groovy method: .toInteger();

This is my code which is converting string to integer successfully.

def Guest\_Count = WebUI.getAttribute(findTestObject(‘Object Repository/Locators/Events/Shift Grid Panel/Guest\_Count\_Total’),

```
        'value')

    def Guest\_Count\_int = Guest_Count.toInteger()

    WebUI.println('Guest Count is: ' + Guest_Count)

    WebUI.comment('Verify that projected shifts are equal to as expected. Get the total number of rows in shift grid and calculate the projected shifts and verify with actual projected shifts ')

    for (def index = 1; index <= Number\_of\_Rows; index++) {

        def Guest\_to\_Shift_Ratio = DriverFactory.webDriver.findElement(By.xpath(('//table\[@class=\\'k-selectable\\'\]/tbody/tr\[' + 

                index) + '\]/td\[2\]')).getText()

        if (Guest\_to\_Shift_Ratio == 'complex') {

            WebUI.println('Guest to shift ratio is: ' + Guest\_to\_Shift_Ratio)

            continue

        }

        def Guest\_Ratio = Guest\_to\_Shift\_Ratio.toInteger()

        WebUI.println('Guest to shift Ratio is: ' + Guest_Ratio)

        Expected\_Projected\_Shifts = Guest\_Count\_int.intdiv(Guest_Ratio)
```

---

<div class="post-metadata">

**Author:** ![Dibyasankar\_Sendha1](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@Dibyasankar\_Sendha1](https://forum.katalon.com/u/Dibyasankar_Sendha1)\
**Post date:** [August 9, 2018, 3:24pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/6 "2018-08-09T15:24:49Z")

</div>

Its not working for me. Any additional package needs to be added. Please suggest.

---

<div class="post-metadata">

**Author:** ![Bhawna\_Rani](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/bhawna_rani/32/16104_2.png) [@Bhawna\_Rani](https://forum.katalon.com/u/Bhawna_Rani)\
**Post date:** [August 9, 2018, 6:10pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/7 "2018-08-09T18:10:35Z")

</div>

Try with importing this package  
import java.lang.Integer as Integer

You may see strike line on ‘tointeger()’ method which is totally fine. Or if it still doesn’t work, can you post what error are you getting?

---

<div class="post-metadata">

**Author:** ![Sanhita\_Bagchi](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@Sanhita\_Bagchi](https://forum.katalon.com/u/Sanhita_Bagchi)\
**Post date:** [November 28, 2018, 3:03pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/8 "2018-11-28T15:03:47Z")

</div>

Thanks Bhawna,

It worked for me as well.

---

<div class="post-metadata">

**Author:** ![asnayal](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@asnayal](https://forum.katalon.com/u/asnayal)\
**Post date:** [March 18, 2019, 12:12pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/9 "2019-03-18T12:12:08Z")

</div>

Hi Bhawna,

I am getting below error while converting string to int:

scaCostInt = scaCosts.toInteger() FAILED.  
Reason:  
java.lang.NumberFormatException: For input string: “2913013.00”

I already added “import java.lang.Integer as Integer” package.

It would be great if you can help me out on this.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [March 18, 2019, 1:22pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/10 "2019-03-18T13:22:08Z")

</div>

Hello,

your input string is not Integer, but number with floating point (Float / Double).

> String s = “2913013.00”  
> Double d = Double.parseDouble(s)

You should work with this data type instead of int.

---

<div class="post-metadata">

**Author:** ![asnayal](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@asnayal](https://forum.katalon.com/u/asnayal)\
**Post date:** [March 19, 2019, 7:57am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/11 "2019-03-19T07:57:24Z")

</div>

Thank you very much Marek. It solved my main problem.

But Settext statement is not able to enter this Double value into the desired text box. Neither giving any error nor its entering any value to the corresponding field.  
Though I have done some workaround for this, I mean writing that value into my test data sheet(excel), doing needed calculation there and writing back from that excel sheet to the text field. But I think there must be a easy way to entering such types of values.

Thanks and Regards  
Anil

---

<div class="post-metadata">

**Author:** ![Marek\_Melocik](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/marek_melocik/32/371_2.png) [@Marek\_Melocik](https://forum.katalon.com/u/Marek_Melocik)\
**Post date:** [March 19, 2019, 9:04am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/12 "2019-03-19T09:04:53Z")

</div>

WebUI.setText method expects string value, so you get an error. You can convert your double value to string using toString() method.

```
Double finalResult = // your calculation here
WebUI.setText(testObject, finalResult.toString())
```

---

<div class="post-metadata">

**Author:** ![asnayal](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@asnayal](https://forum.katalon.com/u/asnayal)\
**Post date:** [March 19, 2019, 2:03pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/13 "2019-03-19T14:03:11Z")

</div>

Thank you so much Marek for the prompt reply.

But this conversion (double value to string) not working properly for decimal values as the “.” is removed which makes incorrect value entered into the text field. My final value is $323,344,4.43 but after conversion i am getting $323,344,443

---

<div class="post-metadata">

**Author:** ![asnayal](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@asnayal](https://forum.katalon.com/u/asnayal)\
**Post date:** [April 1, 2019, 3:21pm UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/14 "2019-04-01T15:21:58Z")

</div>

Hi Everyone,

My above problem with decimal values is due to the restrictions on the text field in my app, for entering “.” which I came to know later. It was not related to Katalon Studio.

I am really sorry to bother you.

Regards  
Anil

---

<div class="post-metadata">

**Author:** ![phtan84sally](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@phtan84sally](https://forum.katalon.com/u/phtan84sally)\
**Post date:** [November 25, 2019, 2:48am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/15 "2019-11-25T02:48:59Z")

</div>

HI,

Im new to Katalon.

I have similar issue as above.  
My test steps is as:

1. Get text “Bank Fee”
2. Convert String to number
3. Get text “offline Bank Fee”
4. Convert String to number.
5. Get sum of New\_BankFee = “Bank Fee” + “Offline Bank Fee”

//Get Bank Fee  
WebUI.scrollToElement(findTestObject(‘Ob\_Bank Transaction Log/Browse/\_Listing/td\_Total\_BankFee’), 2)

String Total\_BankFee = WebUI.getText(findTestObject(‘Ob\_Bank Transaction Log/Browse/\_Listing/td\_Total\_BankFee’), FailureHandling.STOP\_ON\_FAILURE)

def x = Double.parseDouble(Total\_BankFee)

WebUI.printIn(‘Total Bank Fee is:’ + Total\_BankFee)

//‘0 = Offline Bank Fee’  
WebUI.waitForElementClickable(findTestObject(‘Ob\_Bank Transaction Log/Browse/filter\_PaymentType’), 2)

WebUI.selectOptionByValue(findTestObject(‘Ob\_Bank Transaction Log/Browse/filter\_PaymentType’), ‘0’, true)

WebUI.waitForElementClickable(findTestObject(‘Ob\_Bank Transaction Log/Browse/btn\_Apply’), 2)

WebUI.click(findTestObject(‘Ob\_Bank Transaction Log/Browse/btn\_Apply’), FailureHandling.STOP\_ON\_FAILURE)

WebUI.delay(3)

WebUI.waitForElementClickable(findTestObject(‘Ob\_Bank Transaction Log/Browse/\_Listing/td\_TotalAmount’), 2)

//String s = Total\_OfflineBankFee  
String Total\_OfflineBankFee = WebUI.getText(findTestObject(‘Ob\_Bank Transaction Log/Browse/\_Listing/td\_TotalAmount’), FailureHandling.STOP\_ON\_FAILURE)

def y = Double.parseDouble(Total\_OfflineBankFee)

WebUI.printIn(‘Total Offline Bank Fee is:’ + Total\_offlineBankFee)

I got this error:  
Test Cases/TC\_Bank Transaction/TC\_Payment Report/TC\_TEST2 FAILED.  
Reason:  
java.lang.NumberFormatException: For input string: “-636,705.00”  
at java\_lang\_Double$parseDouble.call(Unknown Source)  
at TC\_TEST2.run(TC\_TEST2:104)  
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)  
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)  
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)  
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)  
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)  
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)  
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)  
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)  
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)  
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)  
at TempTestCase1574649636422.run(TempTestCase1574649636422.groovy:23)

Anyone can help me?

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [November 25, 2019, 2:52am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/16 "2019-11-25T02:52:23Z")

</div>

> [@phtan84sally](#):
>
> java.lang.NumberFormatException: For input string: “-636,705.00”

I think the presence of the `,` in the string makes it invalid to be a Double.

Please try the accepted answer here:

> <https://stackoverflow.com/questions/24880563/convert-to-int-from-string-of-numbers-having-comma>

---

<div class="post-metadata">

**Author:** ![phtan84sally](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@phtan84sally](https://forum.katalon.com/u/phtan84sally)\
**Post date:** [November 26, 2019, 1:43am UTC](https://forum.katalon.com/t/how-to-convert-string-to-int-in-katalon/8371/17 "2019-11-26T01:43:38Z")

</div>

It works now by removing the comma.  
Thank you very much.
