Qbasicnews.com
April 03, 2020, 08:08:52 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Comparing numeric strings with leading zeros - QB bug maybe?  (Read 1383 times)
Moneo
Na_th_an
*****
Posts: 1971


« on: February 26, 2006, 10:38:27 PM »

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?

*****
Logged
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #1 on: February 26, 2006, 11:50:00 PM »

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.
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #2 on: February 27, 2006, 03:53:21 PM »

You're absolutely right, Plasma, thanks.

I must be getting a little punchy in my old age. :wink:
*****
Logged
anarky
Been there, done that
*****
Posts: 1231


The Blobworld Comics King


« Reply #3 on: February 27, 2006, 10:03:50 PM »

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

>anarky
Logged

Screwing with your reality since 1998.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!