Convert string to int


#1

After I have used ‘get text’ function to retrieve a number from a website, I would like to convert the number from string to int. The number is now in the format 1.495,20.

I have tried using .Replace to remove “.” and “,”.

When I replace “.” with “” I get 1495,20. But then, when I try replacing “,” with “” I get an error saying that Open Strict On does not allow implicit conversions from string to int.

How do I then get a variable with the number 149520?


#2

Hi there. You might need to do a convert also to resolve the error.
For example,
CInt(“1.495,20”.Replace(".","").Replace(",","").Trim)
or
Convert.ToInt32(“1.495,20”.Replace(".","").Replace(",","").Trim)

I put in .Trim incase you have any spaces.

Another method is to extract only the digits out.
CInt(String.Concat(“1.495,20”.Where(AddressOf Char.IsDigit)))
or
CInt(System.Text.RegularExpressions.Regex.Replace(“1.495,20”, “[^0-9]”, “”))

Regards!


#3

You can also try something like

Int32.parse(“1.495,20”.Replace(".","").Replace(",",""))


#4

Hi,

You can try Regex.Replace and Convert.ToInt
For Example…
Convert.ToInt32(Regex.Replace(“1,245.00”,"[,|.]","").ToString)

Regards,


#5

This worked like a charm! And thank you for the additional input, it will be handy later on :slight_smile:


#6


try this one…