GLBasic forum

Main forum => GLBasic - en => Topic started by: Goos on 2011-Mar-10

Title: How to VAL an string
Post by: Goos on 2011-Mar-10
I want to get a string filled with numbers (like "1234.45") in a number.
I used it in other BASIC's with a=VAL("1234.45") but it isn't working in GLBASIC.
What am i missing.
Title: Re: How to VAL an string
Post by: Minion on 2011-Mar-10
if you have a$="1234.5"  you can just say a=a$ and this works
Title: Re: How to VAL an string
Post by: Goos on 2011-Mar-11
Thanks that did the trick. :good:
Title: Re: How to VAL an string
Post by: spicypixel on 2011-Dec-30
I have been searching for an equivalent to VAL for a while. I know this is an old topic but as there's no actual command for it and although what Minion has mentioned works it should be mentioned in the documentation possibly under FORMAT$ in the Refs:

It could be worth even having a command called VAL("") that does nothing more than a = a$ but is at least listed? Thoughts???

:|
Title: Re: How to VAL an string
Post by: spacefractal on 2011-Dec-30
for sending to functions, val might been usable, because I have seen GLBASIC does not allways convert strings correctly to a integer, and hence its only add values end of it, rather mathing it. Howover normally I remember extensive use of () does work.....

So here VAL() can been nice use for use with functions (sending to them) to make sure its got conveted to integer, before any math. Its the only reason val() can been useful.
Title: Re: How to VAL an string
Post by: MrTAToad on 2011-Dec-30
I've had no problems with the conversion of strings to integers - have you get any example code where it doesn't work properly ?
Title: Re: How to VAL an string
Post by: spacefractal on 2011-Dec-30
Doing its normally to a variable, there is no problems at all. Its just a special case its might have its value.

Its can been sometimes happens when you working with functions. Here is a example, which it might fail:
printtext$(somevalue$(a)+b)

Here I have seen b something added like its was a string and did not + at all. Here a printtext$(VAL(somevalue$(a))+b) would force its to convert to a value to the end to make sure all values is a integer. I remember and thinking its was something like that, a string to variable convert could fail. Howover its far from comon.

PS. So its not really a bug, just some behaiver of when glbasic convert it to c++ I think, and hence val() sometimes can help.

PPS. Last edit, I think I have formatted its correctly without misunderstanding.
Title: Re: How to VAL an string
Post by: Kitty Hello on 2011-Dec-31
A$="1"
B$=A$+7
The + Operator of string and integer returns a string, concatenated.
B$="17" thus!
Title: Re: How to VAL an string
Post by: spicypixel on 2011-Dec-31
Quote from: Kitty Hello on 2011-Dec-31
A$="1"
B$=A$+7
The + Operator of string and integer returns a string, concatenated.
B$="17" thus!

Makes sense but I guess a good reason to have a VAL command, especially as we can convert a number to a string but not the other way around.
Title: Re: How to VAL an string
Post by: Kitty Hello on 2012-Jan-02
I see. For ints you can use INTEGER(a$), but for floats there's no command, yet. On the TODO.
Title: Re: How to VAL an string
Post by: MrTAToad on 2012-Jan-03
Yes, I've just come across this problem myself :)
Title: Re: How to VAL an string
Post by: Kitty Hello on 2012-Jan-04
Function tofloat: f
Return f
Endfunction
Title: Re: How to VAL an string
Post by: spacefractal on 2012-Jan-04
actuelly we could just doing a VAL function by yourself using INTEGER as well tofloat functions....
Title: Re: How to VAL an string
Post by: Slydog on 2012-Jan-04
I remember the good old days with VB6, you could VAL any string, even with extra non-numeric characters.

I created this 'VAL()' routine that mimics VB6's VAL command:
(except mine accepts non-numeric characters before the number!)
Code (glbasic) Select
FUNCTION VAL#: string$
CONSTANT numbers$ = "0123456789."
LOCAL ix%
LOCAL result$ = "", result#

// Strip out any non-numeric characters
FOR ix = 0 TO LEN(string$)-1
// Valid?
IF INSTR(numbers$, MID$(string$, ix, 1)) >= 0
INC result$, MID$(string$, ix, 1)
ELSE
INC result$, " "
ENDIF
NEXT

result = result$
//DEBUG "VAL(" + CHR$(34) + string$ + CHR$(34) + ") -> result: [" + result + "]\n"
RETURN result
ENDFUNCTION


OUTPUT
Code (glbasic) Select
VAL("123.456ABC")    -> result: [123.456]
VAL("ABC123.456ABC") -> result: [123.456]
VAL("$123.456")      -> result: [123.456]
VAL("123.4.56")      -> result: [123.4]
VAL("ABC")           -> result: [0]


[Edit] - For fun, I replaced my guts of the routine with just "result = string$" to compare.
The results were the exact same, except when starting with a non-numeric character (examples 2 and 3), which resulted in '0'.
I had assumed GLBasic wouldn't like non-numeric characters ANYWHERE, like in .Net.  Good to know.
Title: Re: How to VAL an string
Post by: hardyx on 2012-Jan-05
Another way to implement the VAL() function:

Code (glbasic) Select
GLOBAL number;
number = VAL("1.23456789")

PRINT number, 10, 10
SHOWSCREEN
KEYWAIT

FUNCTION foo: // ends the main code
ENDFUNCTION

INLINE
extern "C" { double atof(const char *string); }
ENDINLINE

FUNCTION VAL#: string$
INLINE
DGInt result = atof(string_Str.c_str());
return result;
ENDINLINE
ENDFUNCTION
Title: Re: How to VAL an string
Post by: Kitty Hello on 2012-Jan-09
Really,
Code (glbasic) Select

Function tofloat: f
Return f
Endfunction

works the best, because it works with all data types (except TYPEs)