Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing numeric strings with leading zeros - QB bug maybe?
#1
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?

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

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

>anarky
Screwing with your reality since 1998.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)