Convert Negative String to Integer


#1

Hi all,

Iā€™m reading multiple values from an Excel source file, which I then need to convert to Integers in UiPath.

The Convert.ToInt32 method works fine for positive numbers, but fails for negative numbers.

I need to be able to work with both positive & negative integers, so is there an alternative code to work around this?

Thanks in advance for any help! :slight_smile:

Regards,
Dan


#2

It should work. Are you getting any error messages?

If you see Dashes for negative sign, convert it to hyphen (string.replace)


#3

Hi @dlp1980,
If you have string values in excel file first convert them to integer.
Use System.Math.Abs(Number as Integer).ToString to convert if from negative to positive.
It will work if you try to convert from positive also.

Regards,
Jiban


#4

Use Int32.Parse instead of Convert.
In general:
use Convert.ToYourType when you need to change variable type, f.e. int to double, or object to underlying type.
Use YourType.Parse if you need to interpret the value as something else (usually from string to something else).

Note that Parse can sometimes produce weird results if you get to corner case. Not for ints, but Date.Parse really pushes the limits of what is a date (and fills in missing parts in some cases).