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 / Simple math question.... algorythm to extract seperate digits from a number..

Author
Message
Mark Garrett
Reviewed AGK on Steam
19
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 24th Nov 2013 02:01 Edited at: 24th Nov 2013 02:04
Simple math question.... algorythm to extract seperate digits from a number..one-by-one, one at a time.

I know a number is not a string, but I would like a method to extract the single digits of a number, anyone know how I could do it?

I know if it was a word that I could extract seperate letters of the word one-by-one, but how could I do that with a number?

such as...


'number' = 234 * 768 .... (variable 'number' = 234 times 768)

1st digit of 'number' = ?
2nd digit of 'number' = ?
3rd digit of 'number' = ?
etc. etc.

What would be the best way to do it?

.
Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 24th Nov 2013 02:27
The least significant (last) digit, assuming base 10, would be number % 10 (or mod instead of the % sign for modulo in DBPro). Now that you know that you can subtract number by that last digit, then either take the new number % 100 (this will return 30 in your case) or divide it by 10 and do % 10 again (which will give 3). Repeat this process until you run out of digits (all your extracted digits added together = the original number).


"Why do programmers get Halloween and Christmas mixed up?"
Mark Garrett
Reviewed AGK on Steam
19
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 24th Nov 2013 04:51 Edited at: 24th Nov 2013 05:21
Oh, here, going on your inspiration, I concocted this one..

It computes up to 18 digits, so, because a 'double integer' only goes to about 19 digits.


.
Dia
20
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 24th Nov 2013 08:46 Edited at: 24th Nov 2013 08:48
Is there anything actually stopping you from using the string commands to parse your number?

i.e. turn the number into a string, and then read off each of the individual digits (you can turn these individual characters back into numbers if you want to use maths manipulation on them afterwards)

something like:



This runs into the same issue with double integer size limitation, but the expandable array can really hold any number of digits

I stored the digits in the numerals() array because it makes it easy to manipulate, and well, because I like arrays (don't worry, I go to meetings)

This is not the Sig you are looking for....
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 24th Nov 2013 22:04
Just use str$ to convert number to string then loop through each character one by one. Same as what Dia above says although he is a bit long winded in extracting characters, just use mid$.



Boo!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 24th Nov 2013 23:07
10pts to Spooky


Formerly OBese87.
Dia
20
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 25th Nov 2013 00:29
Aww man, all this time I just assumed MID$ worked the same as LEFT$ and RIGHT$, and returned 'n' characters out of the middle of a string.

Thanks spooky!

This is not the Sig you are looking for....
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Nov 2013 04:23
A somewhat shameless self promotion here since it's somewhat related(mixing strings and numbers). If you'd like to add numbers together that are in a string form:
http://forum.thegamecreators.com/?m=forum_view&t=177633&b=6

Login to post a reply

Server time is: 2025-05-17 10:47:34
Your offset time is: 2025-05-17 10:47:34