Qbasicnews.com
Comparing numeric strings with leading zeros - QB bug maybe? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: QBasic (http://qbasicnews.com/newforum/forum-4.html)
+--- Forum: QB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-11.html)
+--- Thread: Comparing numeric strings with leading zeros - QB bug maybe? (/thread-8941.html)



Comparing numeric strings with leading zeros - QB bug maybe? - Moneo - 02-27-2006

I've always been under the impression that in QuickBasic you could compare numeric strings, with or without leading zeros.

In this particular case, I was making sure that the input string would fit into an integer. The string had already been checked to make sure that all the digits were numeric. Example:
Code:
rem ... x$ is the string containing the input numeric digits.
if x$ > "32767" the print "Error: number is not an integer"
The above works fine as long as x$ does not contain any leading zeros. For example, if x$ contains "032768" or "0099999" then the above IF statement does not print the error message.

However, if you do the following, it works fine:
Code:
rem ... x$ is the string containing the input numeric digits.
dim d as double
d = val(x$)
if d > 32767 the print "Error: number is not an integer"

WHY?

*****


Comparing numeric strings with leading zeros - QB bug maybe? - Plasma - 02-27-2006

This is not a bug. QB doesn't care whether your strings are "numeric" or not; they are all compared using the ASCII value of the characters. Since "0" (48) is less then "3" (51), any string that starts with a "0" will be "less than" any string that starts with a "3".

Your second code segment works because you are first converting the string to a number, and then comparing numbers.


Comparing numeric strings with leading zeros - QB bug maybe? - Moneo - 02-27-2006

You're absolutely right, Plasma, thanks.

I must be getting a little punchy in my old age. :wink:
*****


Comparing numeric strings with leading zeros - QB bug maybe? - anarky - 02-28-2006

Yes, I was going to say "alphbetical, or ASCIIbetical order". I've always used the second method, convert then compare.

>anarky