Convert Negative String to Integer

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:


It should work. Are you getting any error messages?

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

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.


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).

1 Like