QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Asil on February 11, 2020, 07:12:48 pm

Title: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: Asil on February 11, 2020, 07:12:48 pm
i have 2 integer variable assigned 3 and 5 and i wanna make 1 integer variable and assign it to 35 by combining to integer value.. help pls. thnks..
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: SMcNeill on February 11, 2020, 07:30:10 pm
Num1 = 23
Num2 = 45
Num3 = VAL(_TRIM$(STR$(Num1)) + _TRIM$(STR$(Num2)))
PRINT Num3
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: TerryRitchie on February 11, 2020, 08:46:11 pm
Num1 = 3
Num2 = 5
Num3 = Num1 * 10 + Num2
Print Num3
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: SMcNeill on February 11, 2020, 09:15:25 pm
Num1 = 3
Num2 = 5
Num3 = Num1 * 10 + Num2
Print Num3

The only thing with this answer is that Asii mentioned adding integer to integer.  If Num2 is greater than 10, you wouldn’t get a proper result; which is why I turned them into strings and added them.  ;)
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: TerryRitchie on February 11, 2020, 09:41:34 pm
Yep, I see what you mean. I provided an example of how to use power of 10 to achieve an answer for the specific scenario he gave.

With my example his numbers would always need to be 0 through 9.

Num1 = 2
Num2 = 5
Num3 = 8
Combined = Num1 * 100 + Num2 * 10 + Num3
PRINT Combined
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: Asil on February 12, 2020, 04:34:04 am
i did in this way it works atm. i got 2 string together and convert to int with Val function...
NumberCombined = VAL(MID$(t$, 1, 1) + MID$(t$, 2, 1))
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: Petr on February 12, 2020, 04:09:21 pm
Hi.

This is my solution for INTEGERS. At first I was sad that QB64 does not have a function that returns the length of a number (number of digits) without conversion to a string, but then I sat down a little in the chair and realized that it could simply be written as a custom function. This, as it is, works for integers, if the function were to return the length, including the places to the right of the decimal point, it would have to be modified. About how do it for decimals, I'm now thinking. But, this is really interest thing.
Code: QB64: [Select]
  1. num1 = 157
  2. num2 = 311
  3. 'result 157311
  4.  
  5. l = LENGHT(num2) 'this is NUMERIC alternative (fastest) for slow STRING operations >>> 'l = LEN(STR$(num2)) - 1
  6. PRINT num1 * 10 ^ l + num2
  7.  
  8. FUNCTION LENGHT (number)
  9.     IF number = 0 THEN LENGHT = 1: EXIT FUNCTION
  10.     IF number <> number \ 1 THEN PRINT "Number is not integer": EXIT FUNCTION
  11.     L = 1
  12.     DO UNTIL number / L < 1
  13.         L = L * 10
  14.         LENGHT = LENGHT + 1
  15.     LOOP
  16.  
Title: Re: assign 2 integers variable value to 1 integer variable value? like 3 and 5 to 35
Post by: Richard Frost on February 12, 2020, 04:50:34 pm
I like to point out the obvious.

Code: QB64: [Select]
  1.   INPUT "Gimme a number ", n#
  2.   IF n# = 0 THEN EXIT DO
  3.   log10 = LOG(n#) / LOG(10#) + 1
  4.   PRINT INT(log10); " digits"
  5.