# How to assert integer value extracted from string?

**URL:** <https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147>\
**Category:** Robot Framework\
**Created:** [2 September 2023 12:12 UTC](https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147 "2023-09-02T12:12:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![savadibrahim](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/savadibrahim/32/2963_2.png) [@savadibrahim](https://forum.robotframework.org/u/savadibrahim)\
**Post date:** [2 September 2023 12:12 UTC](https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147/1 "2023-09-02T12:12:47Z")

</div>

Im a beginner into RF and just starting to get hang of things

My RF code

AssertOrder # IN ORDER DOCUMENT PAGE  
${orderdocamt} Get Text ${orderdoctotal}  
${orderdocamtint} Convert Currency Text To Integer ${orderdocamt}  
Should Be Equal ${orderdocamtint} ${amount}  
Log To Console Checked Order total Equal to Sales Document amount

This is the custom keyword im using  
Convert Currency Text To Integer  
[Arguments] ${currency\_text}  
${currency\_values} Get Regexp Matches ${currency\_text} $\d+(?:,\d{3})\*(?:.\d{2})?  
${integer\_values} Create List  
FOR ${value} IN @{currency\_values}  
${integer\_part} ${decimal\_part} Split String ${value} .  
${integer\_part} Remove String ${integer\_part} ,  
${integer\_value} Convert To Integer ${integer\_part}${decimal\_part}  
Append To List ${integer\_values} ${integer\_value}  
END  
[Return] ${integer\_values}

When i log to console,it is outputting for ${orderdocamtint} . I tried many things but couldnt understand why integer value is not returning … It is returning blank .Where do i go wrong

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [2 September 2023 15:38 UTC](https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147/2 "2023-09-02T15:38:20Z")

</div>

You are returning a list and then using Should Be Equal which does not work with lists.

I am with mobile, so cannot test your code, but you should edit and add a sample input for us to replicate.

---

<div class="post-metadata">

**Author:** ![savadibrahim](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/savadibrahim/32/2963_2.png) [@savadibrahim](https://forum.robotframework.org/u/savadibrahim)\
**Post date:** [3 September 2023 05:19 UTC](https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147/3 "2023-09-03T05:19:31Z")

</div>

AssertOrder  
${orderdocamtint} Convert Currency Text To Integer $1500.50  
Should Be Equal ${orderdocamtint} 1500.50  
Log To Console Checked Order total Equal to Sales Document amount

This is the custom keyword im using  
Convert Currency Text To Integer  
[Arguments] ${currency\_text}  
${currency\_values} Get Regexp Matches ${currency\_text} $\d+(?:,\d{3})\*(?:.\d{2})?  
${integer\_values} Create List  
FOR ${value} IN @{currency\_values}  
${integer\_part} ${decimal\_part} Split String ${value} .  
${integer\_part} Remove String ${integer\_part} ,  
${integer\_value} Convert To Integer ${integer\_part}${decimal\_part}  
Append To List ${integer\_values} ${integer\_value}  
END  
[Return] ${integer\_values}

---

<div class="post-metadata">

**Author:** ![savadibrahim](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/savadibrahim/32/2963_2.png) [@savadibrahim](https://forum.robotframework.org/u/savadibrahim)\
**Post date:** [3 September 2023 05:22 UTC](https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147/4 "2023-09-03T05:22:03Z")

</div>

How do i convert the list returned to scalar variable ?

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [3 September 2023 12:21 UTC](https://forum.robotframework.org/t/how-to-assert-integer-value-extracted-from-string/6147/5 "2023-09-03T12:21:58Z")

</div>

Hi @savadibrahim,

1. I had a go at it, first issue seems to be with you regular expression, it’s not returning any matches, but I’m sure what you wanted to get out of the regex?  
A $ has a special meaning in regex, it’s the end of string character, so if you want to include it you need to escape it (\$) but from your keyword it looks like you didn’t want it?  
I put your example string and your regex as is into regex101 for you to play around and figure out the regex you need → [regex101: build, test, and debug regex](https://regex101.com/r/aczvG1/1)

2. When you get the regex returning what you want, the second issue is I expect your `Convert To Integer` line will convert the dollar amount to integer cents, and append that to the list that’s returned? so when you compare 150050 to 1500.50 `Should Be Equal` will fail, is that what you want?

3. on your `Should Be Equal` line in your test, as @HelioGuilherme66 mentioned, you are comparing a list to a value, perhaps you want to compare the fist item in the list, something like this:

```python
Should Be Equal ${orderdocamtint}[0] 1500.50

```

Finally FYI, use three back ticks (```) on the line before and after your code for a code block so your white space is retained. I guess you had something like this:

```python
***Settings***
Library String

***Test Cases***
AssertOrder
	${orderdocamtint} Convert Currency Text To Integer $1500.50
	Should Be Equal ${orderdocamtint} 1500.50
	# Log To Console Checked Order total Equal to Sales Document amount

***Keywords***
# This is the custom keyword im using
Convert Currency Text To Integer
	[Arguments] ${currency_text}
	${currency_values} Get Regexp Matches ${currency_text} $\d+(?:,\d{3})*(?:.\d{2})?
	${integer_values} Create List
	FOR ${value} IN @{currency_values}
		${integer_part} ${decimal_part} Split String ${value} .
		${integer_part} Remove String ${integer_part} ,
		${integer_value} Convert To Integer ${integer_part}${decimal_part}
		Append To List ${integer_values} ${integer_value}
	END
	[Return] ${integer_values}

```

Anyway with a bit of playing I’m guessing you wanted a regex something like `\$([\d\.,]*)`, but you need to escape the escape character in robot framework, so that becomes `\\$([\\d\\.,]*)`

So this is my version:

```python
***Settings***
Library String
Library Collections

***Test Cases***
AssertOrder
	${orderdocamtint} Convert Currency Text To Integer $1500.50
	# Should Be Equal ${orderdocamtint} 1500.50
	Should Be Equal ${orderdocamtint}[0] 1500.50
	# Log To Console Checked Order total Equal to Sales Document amount
	# ${orderdocamtint} Convert Currency Text To Integer $1500.50 $13.50 something else 1234568 $987654.32 and the non decimal $13

***Keywords***
# This is the custom keyword im using
Convert Currency Text To Integer
	[Arguments] ${currency_text}
	# ${currency_values} Get Regexp Matches ${currency_text} $\d+(?:,\d{3})*(?:.\d{2})?
	${currency_values} Get Regexp Matches ${currency_text} \\$([\\d\\.,]*) 1
	${integer_values} Create List
	FOR ${value} IN @{currency_values}
		${integer_part} ${decimal_part} Split String ${value} \.
		${integer_part} Remove String ${integer_part} ,
		${integer_value} Convert To Integer ${integer_part}${decimal_part}
		Append To List ${integer_values} ${integer_value}
	END
	[Return] ${integer_values}

```

As I mentioned in point 2 it fails with `150050 != 1500.50`

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/d/de57ef7756dcfa05ebc6f2ceb990672e1003194b.png)

1. Note this will still have an issue with whole dollar values i.e. $13 (with no cents)

Hope that helps,

Dave.
