# How to compare two numbebrs

## Recommended Posts

Hi,

First I will have to read a value. Then read first two characters of the value. Then compare the value to see it it is greater than 8. First two characters sometimes can be null, string, alphanumeric or numeric.

```		miless = "2mile(s)"
myMile = mid(miless,1,2)
print myMile
If myMile>8 then
print "a>b"
End If```

It is always returning true even though first two characters are not numbers or less than 8. How can I do it?

Edited by likecoding
##### Share on other sites

If you want to read the first 2 characters you can use left:

You're comparing characters and numbers.  When you do that it converts the characters to numbers, and if you look at a character table, "A" is 65 and "a" is 97, so it's never going to be less than 8.  If you want to compare them like numbers then convert the characters to numbers first.  You can use CInt.

##### Share on other sites

```miless = "2mile(s)"
myMile = Left(miless,2)
print myMile
myMile = cint(myMile)
print myMile
If myMile > 8 then
print "a>b"
End If```

first line miless variable can have value like "20 miles, 35 miles, empty, no miles, etc". It works only if first two characters are numeric.

##### Share on other sites

Yeah that's what you're telling it to do, convert the the first 2 characters to a number and check if it's greater than 8.  If that's not what you want to do, what do you want to do?

##### Share on other sites

I am trying to say that if first two characters are like "2m, 3k", it returns true. I mean it prints "a>b". Instead, it should not print any thing.

First two characters can be numbers or strings. If strings(i.e. 2m, 3k, jj) , it should not print "a>b" at all. If numbers (i.e. 11, 44, 08), then print "a>b" if greater than 8, else do not print.

It should print "a>b" if first two characters are numeric and greater than 8. Else, do nor print "a>b".

Edited by likecoding
##### Share on other sites

I am trying to say that if first two characters are like "2m, 3k", it returns true. I mean it prints "a>b". Instead, it should not print any thing.

I can see what it's doing, the code is there.  That's what you're telling it to do.  If you want it to do something else, then you need to tell it to do something else.  If you want to check the 2 characters before you convert them, one option would be to use regular expressions to check if both characters are numbers, or anything else.  VBScript might have some other functions available that can do those kinds of checks also.