Page 1 of 1

Val() Problems

Posted: Wed Jan 10, 2007 11:14 am
by BinoX
When using Val() if the input is not a number the function returns 0... however 0 IS a number..

so if I was to Val("0") it would return the same as Val("a").. so the statement

if Val(Variable)
DoSomething()
endif

wouldn't work if Variable was "0"

Posted: Wed Jan 10, 2007 11:19 am
by Trond
So what do you want it to return if you pass it not a number?

Posted: Wed Jan 10, 2007 11:25 am
by Derek
Well, you can't return -1 because that's a number, bit stuck really.

Posted: Wed Jan 10, 2007 12:17 pm
by BinoX
I dunno... Perhaps add a StringIsNumber() function that returns true or false..

i.e.
StringIsNumber("0") would be True
and
StringIsNumber("a") would be False...

Posted: Wed Jan 10, 2007 12:25 pm
by Derek
Incidently, Debug Val("10abc") returns 10 when you would think it would return 0 as technically speaking it isn't a number.

Posted: Wed Jan 10, 2007 12:54 pm
by Kaeru Gaman
IsNumeric()-discussion in the german forum with some code examples:
http://www.purebasic.fr/german/viewtopic.php?t=2203
the codes should be international....

PS:
btw, the Val() function is completely correct, because it is defined the way it works now.

Posted: Wed Jan 10, 2007 4:39 pm
by Michael Vogel
A lot of things could be added for more comfort, for example...

:idea: Val?() returns number of "correct" characters for a number (e.g. 4 for the string "10.1abc")

:idea: Val("%111") and Val("$affe") returns also numbers

Posted: Wed Jan 10, 2007 4:46 pm
by Kaeru Gaman
but that would have nothing to do with the basic function of Val()

would be easy to implement some CountNumbers(), Bin2Dec() or Hex2Dec() by yourself.

but the function of Val() should remain unchanged, because it works like it was in BASIC ever since.

Posted: Wed Jan 10, 2007 5:00 pm
by Psychophanta
@BinoX
There is a "general discussion" section. Why do you post this in the "Bug Reports" one? Do you really though this was a bug? :?

Re: [PB 4.02] Val() Error

Posted: Wed Jan 10, 2007 9:02 pm
by PB
Val("a") returns 0 on other Basics, such as the C64 and Visual Basic, so there's
no need to alter its behaviour for PureBasic. It's been that way since the 1980s
so you'll just need to figure a way around it, like everyone else has been doing
for over 20 years. ;) Sounds like you need a different coding approach to it.

Posted: Thu Jan 11, 2007 11:48 pm
by BinoX
Bah, So much for being able to do lazy coding for everything...

When I do Val("a") on Visual Basic it just tends to give a runtime error and crash the program... Maybe my VB aint installed right then..

Posted: Thu Jan 11, 2007 11:51 pm
by PB
I only tested that in the VB development environment; I didn't create a VB exe
and run it there. Maybe that's different, but I'm too lazy to test it right now. ;)

Posted: Fri Jan 12, 2007 3:34 pm
by Demivec
When using Val() if the input is not a number the function returns 0... however 0 IS a number..
You would handle the validity of the zero result by checking the string used in Val(string) to see if all of it or at least the first character [Left(string,1)] is ="0" . I know you would have to consider a leading minus sign too. All other results would return the portion at the beginning of the string that was numeric, as it should.


Demivec