Qbasicnews.com

Full Version: Comparing numeric strings with leading zeros - QB bug maybe?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?

*****
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.
You're absolutely right, Plasma, thanks.

I must be getting a little punchy in my old age. :wink:
*****
Yes, I was going to say "alphbetical, or ASCIIbetical order". I've always used the second method, convert then compare.

>anarky