Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to convert STR to INT

Author
Message
globule
22
Years of Service
User Offline
Joined: 13th Aug 2003
Location:
Posted: 13th Aug 2003 10:55
Hello,

How to convert STRING to INTEGER
How to test if a string can be converted into numeric value (function IsNumeric() in VB)
ReD_eYe
23
Years of Service
User Offline
Joined: 9th Mar 2003
Location: United Kingdom
Posted: 13th Aug 2003 11:02
from the help files:

VAL
This command will return an integer number of the string provided by converting the string to a
numerical form.

SYNTAX
Return Float=VAL(String)

i don't think theres a command or function fro your second question but it is probably possible

I have a website!!!
So go there: http://www.freewebs.com/darkb/index.htm
on second thought you probably shouldn't
MrTAToad
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 13th Aug 2003 11:03
Use VAL to convert a string to an integer.
You dont have a way of testing if a string can be completely converted though - it will return a value up until the first invalid character.


Logo design by Indi
Its better than a poke in the eye...
Ratty Rat
23
Years of Service
User Offline
Joined: 24th Sep 2002
Location:
Posted: 13th Aug 2003 13:13
Adding to what MrTAToad said, given that the first character of the string MUST be a number to get a value out you can do the following to see whether the string contains a number as its first character (the only other way would be to loop through the entire string using mid$) :-

if asc(left$(Txt$,1)) >= 48 and asc(left$(Txt$,1)) <= 57 then I = val(Txt$)

"Don`t try to engage my enthusiasm, I don`t have one"
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 13th Aug 2003 22:53 Edited at: 13th Aug 2003 22:54
Just write your own version of the VB IsNumeric() Function. Create a loop based on the length of the string and test each character with the code provided by Ratty Rat above and move to the next char each time thru the loop. If the loop finishes and each char was a numeric value then you can return True (or 1) from your function back to your calling code.

peace
Rustler

How do ya do there son
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 14th Aug 2003 02:11
Here's a basic IsNumeric function. It doesn't handle scientific notation, but I leave that as an exercise to the reader

The important functions are IsNumeric (obviously) and IsDigit. The rest is just fluff for testing.

Login to post a reply

Server time is: 2026-07-22 18:54:12
Your offset time is: 2026-07-22 18:54:12