Qbasicnews.com
March 28, 2020, 05:06:57 PM
 Welcome, Guest. Please login or register. 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Home Help Search Login Register
 Pages: [1] 2 3
 Author Topic: string and numeric variable questions  (Read 7216 times)
jgr
Forum Regular

Posts: 131

 « on: March 22, 2004, 10:14:30 PM »

Can you or how can you let a string variable equal a numeric variable and vice versa.

for example

a\$=num  Where num is inputed

or

num = a\$  the numeric of a\$
 Logged
whitetiger0990
__/--\__

Posts: 2964

 « Reply #1 on: March 22, 2004, 10:17:57 PM »

string\$ = STR\$(num)
num = VAL(string\$)

there.
but the STR\$ adds a space to the left (or right I forget) os if you want you need to LTRIM\$ it.
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
RST
Wandering Guru

Posts: 326

 « Reply #2 on: March 22, 2004, 10:40:50 PM »

STR\$() Adds spaces to both the left *and* right. The one to the left holds the negative sign if needed.
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #3 on: March 23, 2004, 12:10:20 AM »

RST, I don't recall the string of a value having a space on the right end. As a matter of fact I just ran a test, and there is definitely no space on the right end.
*****
 Logged
whitetiger0990
__/--\__

Posts: 2964

 « Reply #4 on: March 23, 2004, 12:26:39 AM »

yup Moneo is correct (((unless by some miraculous event i get killed and a temporal rift opens up that NO DUCT TAPE SHALL CLOSE!)))
Code:
PRINT "a" + STR\$(1) + "a"
PRINT "a" + STR\$(-1) + "a"
 Logged

[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
R@dioman
Ancient QBer

Posts: 410

 « Reply #5 on: March 23, 2004, 02:44:37 AM »

jgr: If you want to get rid of that extra space at the beginning, use LTRIM\$(variable\$). This function returns the string with any spaces removed starting from the left(RTRIM\$ does it from the right). To use it, do this:
variable\$ = LTRIM\$(variable\$)
 Logged

1000101
Ancient Guru

Posts: 519

 « Reply #6 on: March 23, 2004, 07:31:43 AM »

Or, you can use FORMAT\$ to control how the string is generated from the number.

ie:

Code:
PRINT FORMAT\$(12345)
PRINT FORMAT\$(12345, "##,###")
 Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
jgr
Forum Regular

Posts: 131

 « Reply #7 on: March 23, 2004, 09:14:21 AM »

Thanks fella's. I appreciate it, and I am sure there will be more questions to follow.
 Logged
*/-\*

Posts: 3655

 « Reply #8 on: March 23, 2004, 12:01:17 PM »

For QB, I usually just say "screwit" and use something like this:

string\$ = LTRIM\$(RTRIM\$(STR\$(num)))

Old habits die hard I suppose...but there was a time when spaces appeared on both sides. Maybe it's not with integers, maybe it's with singles and doubles? Anyone want to confirm?

For VB, we get to use something like this:

string\$ = Trim(Str(num))

so much easier
 Logged

I'd knock on wood, but my desk is particle board.
oracle
*/-\*

Posts: 3652

 « Reply #9 on: March 23, 2004, 06:58:06 PM »

A space appears when you're converting a positive number to a string, the space is the reserved space for the "-" sign that never appeared.

And you could do this:

Code:
DEF FNtrim(strng\$) = LTRIM\$(RTRIM\$(strng\$))

^^ not sure on the syntax of DEF FN but that's the basis of how I'd make a TRIM\$ function...
 Logged

1000101
Ancient Guru

Posts: 519

 « Reply #10 on: March 23, 2004, 07:05:02 PM »

For QB, I usually just say "screwit" and use something like this:

string\$ = LTRIM\$(RTRIM\$(STR\$(num)))

I used to do that too, but I found FORMAT\$ which does the exact same thing with no string parameter.

ie:

string\$ = FORMAT\$(num)
 Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
oracle
*/-\*

Posts: 3652

 « Reply #11 on: March 23, 2004, 07:09:50 PM »

^^^ I wrote that, not ado

I've never heard of FORMAT\$ before... if that works fine then use that
 Logged

1000101
Ancient Guru

Posts: 519

 « Reply #12 on: March 23, 2004, 07:15:52 PM »

Odd, I just clicked the 'reply with quote' and stripped the rest of the message, I didn't touch the usename.
 Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
*/-\*

Posts: 3655

 « Reply #13 on: March 23, 2004, 07:38:42 PM »

*checks the posts*

Oracle, I wrote that ya momo you never even say "screwit" to my knowledge

I find that I never have use for string formatting...I guess it's different when you need to represent large numbers by triples though...
 Logged

I'd knock on wood, but my desk is particle board.
jgr
Forum Regular

Posts: 131

 « Reply #14 on: March 23, 2004, 08:23:50 PM »

I used

trim\$=str\$(carnumber)
trim\$=ltrim\$(trim\$)
mid\$(raceentry.finishorder,p)=trim\$

print using "###"; val(mid\$(raceentry.finshorder,p,3))

coming from programing business basic years back, qbasic is a lot more work.  In my opion, Using gosub's and goto's is ok as long as you keep everything structured and easy to follow.  Also there is good spagetti and very bad spagetti.
 Logged
 Pages: [1] 2 3