Val() Problems

Everything else that doesn't fall into one of the other PB categories.
BinoX
User
User
Posts: 46
Joined: Mon Jan 31, 2005 11:57 am

Val() Problems

Post 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"
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

So what do you want it to return if you pass it not a number?
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Well, you can't return -1 because that's a number, bit stuck really.
BinoX
User
User
Posts: 46
Joined: Mon Jan 31, 2005 11:57 am

Post 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...
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Incidently, Debug Val("10abc") returns 10 when you would think it would return 0 as technically speaking it isn't a number.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
Michael Vogel
Addict
Addict
Posts: 2820
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post 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
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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? :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: [PB 4.02] Val() Error

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
BinoX
User
User
Posts: 46
Joined: Mon Jan 31, 2005 11:57 am

Post 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..
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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
Post Reply