How to VAL an string

Previous topic - Next topic

Goos

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.

Minion

if you have a$="1234.5"  you can just say a=a$ and this works

Goos

Thanks that did the trick. :good:

spicypixel

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???

:|
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

spacefractal

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.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

MrTAToad

I've had no problems with the conversion of strings to integers - have you get any example code where it doesn't work properly ?

spacefractal

#6
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.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Kitty Hello

A$="1"
B$=A$+7
The + Operator of string and integer returns a string, concatenated.
B$="17" thus!

spicypixel

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.
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Kitty Hello

I see. For ints you can use INTEGER(a$), but for floats there's no command, yet. On the TODO.

MrTAToad

Yes, I've just come across this problem myself :)

Kitty Hello

Function tofloat: f
Return f
Endfunction

spacefractal

actuelly we could just doing a VAL function by yourself using INTEGER as well tofloat functions....
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Slydog

#13
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.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

hardyx

#14
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