Jump to content
Sign in to follow this  
likecoding

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 this post


Link to post
Share on other sites

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

https://www.w3schools.com/asp/func_left.asp

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.

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/functions/type-conversion-functions

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×