Converting an OCR text scrape to a number


Dave Burns
September 01, 2016 13:33 ANSWERED

I am trying to take two screen scrapings and check if one is lesser value than the other, so I need to convert the variables from text to int32, is there an easy way.

Thank you


Cosmin Nicolae September 01, 2016 13:46

There are multiple ways to convert a string into an int. One way is:

[int variable] = CInt([string returned from OCR])

But keep in mind you might want to check if the string is valid for conversion first.


Dave Burns September 01, 2016 19:02
Thanks Cosmin

I tried using that but it was erroring too much 1.18 was parsing to 1. 18 maybe it too much to ask it to produce pure numbers.



Andrzej.Kniola September 02, 2016 01:36
Hi Dave,

You could try trimming whitespace first, if it catches spaces where it shouldn’t - unfortunately quite common with OCR if the element contains commas and similar characters. You could do it either through [yourString].Trim, which will catch only beginning and end, or via [yourString.Replace(" ", String.Empty).

Then to castI would recommend the Convert class. Microsot does not recommend using C[Type] static methods, as they are there mostly for compatibility reason with older vresions of VB.Net (although they are pretty useful, I’ll admit).

If I’m understanding your post correctly, what you have is not an Int, but a floating point number. Int can be used only for whole numbers. And the operation you’ll need is not casting, but parsing.

In that case you could use static methods from the type you want to convert to, specifying the culture to make the decimal separator be recognized correctly (do note that UiPath by default uses US culture if none is specified).

An example could be (written without UiPath, but it’s just an example):

[decimal variable] = Decimal.Parse([yourOcrString].Replace(" ", String.Empty), new CultureInfo.CreateSpecificCulture(“en-gb”))

If the format of the string will not be correct it will throw a FormatException. If you don’t want to have an exception thrown, you could use TryParse method instead.

If you need more detailed example, please feel free - someone here will definitely be able to provide one.




Dave Burns September 02, 2016 14:41
Thanks Andrzej (as always)

Do you know why its erroring saying

CultureInfo.CreateSpecificCulture(“en-gb”) is not defined

Am I missing a library ?




Andrzej.Kniola September 03, 2016 00:15
Probably - you could try adding a reference to System.Globalization (from the Imports tab at the bottom of the screen, near Arguments and Variables), or use a fully qualified name - System.Globalization.CultureInfo

MSDN documentation is here