Page 1 of 1

Help with variables.

Posted: Thu Nov 04, 2010 10:04 am
by Smokehound
I am not sure if Qbasic is able to do this, but here is what I need to do.

I would like to take a variable and break it in to pieces.

Example:

X = 325

I want to cut that up to make three more variables as follows

A = 3
B = 2
C = 5

I assume there is a way to do this using something similar to the LEFT$/RIGHT$ function, but I don?t know what that would be.

Any help would be greatly appreciated.

Thanks,
Jeff

Posted: Thu Nov 04, 2010 2:03 pm
by burger2227
You can convert numbers to string numbers with:

strnum$ = STR$(number%)

STR$ leaves the space on the left with positive values or the minus sign if negative. You can LTRIM$ positive values to get rid of the space.

IF you trim it first you can use:

A$ = LEFT$(strnum$, 1): B$ = MID$(strnum$, 2, 1): C$ = RIGHT$(strnum$, 1)

LEN can tell you how many digits the number is. It will count spaces too.

sign% = SGN(number) can tell the sign with 1 meaning positive and -1 as negative.

DON'T FORGET THAT A, B and C are STRING values that require $ or DIM A AS STRING

To convert strings back to numerical values use numberA% = VAL(A$)

Posted: Fri Nov 05, 2010 3:25 pm
by Smokehound
Thanks for the reply I will give that a shot.

Do you know if there is a way to do it without converting the variable to a string?

Posted: Fri Nov 05, 2010 6:33 pm
by burger2227
Yes, you can use Integer division and MOD, remainder integer division.

number = 325

hundreds = number \ 100 ' = 3

tens = (number MOD 100) \ 10 ' = 25 \ 10 = 2

ones = number MOD 10 ' = 5

When MOD returns 0, the number is evenly divisible using integer division.