String to Integer

I am new to Uipath, i struck in converting string to Integer.

here is the scenario: i am scraping value from my application i .e $100 , like wise i have 3 variables $100 , $20 and $20
now i want to do some mathematical function (1st variable * 2nd variable/100 = 3rd variable)… while executing am getting string variables cannot be multiplied error is showing…

so can some one please help me out me out how i can do this.

Hi @Sohan_Belgur,

Convert string value to int

Variable1=$100
Variable2=$20
Variable3=$20

(Convert.ToInt32(Variable1.replace(“$”,“”))*
Convert.ToInt32(Variable2.replace(“$”,“”))/100) =
Convert.ToInt32(Variable3.replace(“$”,“”))

Regards,
Arivu

1 Like

Hi,

UiPath uses vb.net syntax so most coding questions can be answered through online searches.

Here is my suggestion for you answer:
You need to remove the $ then convert to a number.

variable 3 = Convert.ToDouble(variable1.Replace("$","")) * Convert.ToDouble(variable2.Replace("$","")) / 100

Regards.

2 Likes

@arivu96
I’d advise against using integers to store monetary values, you’ll lose precision.

@ClaytonM
Decimal should be used for monetary values. Double is close, but it loses precision with large/very small numbers (and with multiplications it’s not that hard to get there).

For both I’d say using string.replace to get rid of currency number is not the best approach. It’s also safer to use .Parse (or .TryParse) instead of convert, as it allows specifying culture and number styles.

For example:
(requires System.Globalization namespace to be imported)
decimalFromDollars = Decimal.Parse("$100", NumberStyles.Currency, CultureInfo.CreateSpecificCulture("en-US"));
decimalFromEuro = Decimal.Parse("€100", NumberStyles.Currency, CultureInfo.CreateSpecificCulture("de-DE"));
decimalFromPLN = Decimal.Parse("100 zł", NumberStyles.Currency, CultureInfo.CreateSpecificCulture("pl-PL"))

3 Likes

Good tip on using .Parse. I couldn’t remember the Currency style off the top of my head. But, does that work even if there is no $ in order to be flexible?

Yes, its an allow type flag.
From msdn:

individual field flags define style elements that may be, but do not have to be, present in the string representation of a decimal number for the parse operation to succeed

For currency its currency symbol, thousand and decimal separators, white space (iirc, on mobile now).
Edit - checked
And trailing / ending sign, and parentheses (indicates negative number).

Thanks for your Help, @ClaytonM , When i use above function am getting " “Replace” is not member of UiPath.Genric.values " error. please help me out

“Replace” is a member of strings so you just need to convert your Generic value to a string or use a String variable type.
“.ToString” will convert any non-string value to a string to be used with “Replace” or other string members.
variable1.ToString.Replace( )

Regards.

Hi @Sohan_Belgur,

Change the data type generic Value to string.
Else
use below code
variable1.ToString().Replace("$","")

Regards,
Arivu

OK. I’m reading a CSV file and one of the fields (TaxID) has a dash and I’m trying to remove the dash.
I’ve used this: Convert.ToInt32(CSVCompanyTaxID.Replace(“-”, “”))–But i get an error saying Cannot assign from type ‘System.Int32’ to type ‘System.String’ in Assign acitivity ‘Company Tax ID Remove Dash Variable Generation’.

I’ve used some other expressions but nothing is working right now any suggestions?

Hi @tmartin,

For example
CSVCompanyTaxID=“15352-6752” (string)

The conversation is correct,
Convert.ToInt32(CSVCompanyTaxID.Replace(“-”, “”))
I hope u r assign the this to CSVCompanyTaxID so it’s getting error.

You are converting the value to int so assing the value also must be in int.

Regards,
Arivu

Thanks. So here is the whole picture of what I’m trying to do:
I have the CSVCompanyTaxID as an argument and i have it as a String.
So my first step is to assign the CSVCompanyTaxID to the column in the CSV file. That works. But now I was asked to remove the dash in the field and that is when i put that conversion in my logic.
But i get errors.
image
The first error is:
image
The second error is:
image
The third error is:
image

So what am I needing to do? I know it is simple but I’ve ran out of ideas at the moment.

@tmartin, seems that you have CSVCompanyTaxiD as an integer.

Regards,
Dominic :slight_smile:

CSVCompanyTaxiD change it to string datatype.

Hi @tmartin,

The first error.
In assign activity value use Convert.ToString(dtSEPCSV.Rows(index).item("CompanyTaxId))

Because if suppose you are getting null value it will through error.

Regards,
Arivu

Correct forgot about that. So I changed it back I was messing with so much. Now my issue is my remove the dash- - -and this is my error:
image
Error:
image

Close bracket is missing “)”

@tmartin, you are missing out a bracket.
Dude, I would suggest you to go through the error messages as they are self explanatory.

Regards,
Dominic :slight_smile:

Still getting an error:
image

Hi @tmartin,

Yes because of CSVCompanyTaxiD is string datatype…remove the convert.Toint32

Regards,
Arivu