### Author Topic: Integer Extraction Problem  (Read 8774 times)

0 Members and 1 Guest are viewing this topic.

#### FinaleTI

• Believe in the pony that believes in you!
• CoT Emeritus
• LV10 31337 u53r (Next: 2000)
• Posts: 1830
• Rating: +121/-2
• Believe in the pony that believes in you!
##### Integer Extraction Problem
« on: July 09, 2010, 11:53:51 am »
I'm back! Yay!

I was wondering how to extract a single digit from a number. I need to be able to do this for Nostalgia, as it uses a custom font set, I need a way of testing numbers to display in the custom font. Is there a way to extract a digit from a number, or would I have to send the 1 or 2-byte number to a string.
If so, how would I do that?

Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

#### jnesselr

• King Graphmastur
• LV11 Super Veteran (Next: 3000)
• Posts: 2270
• Rating: +81/-20
• TAO == epic
##### Re: Integer Extraction Problem
« Reply #1 on: July 09, 2010, 11:58:17 am »
It kinda depends on what digit you want.  The last digit in a number is always (num mod base).

The second to last digit is ((num mod base^2)-(num mod base))/base.
The nth digit is ((num mod base^n)-(nm mod base(n-1))/base^(n-1).

Example: 341
341 mod 10 = 1;
((341 mod 100)-(341 mod 10))/10=(41-1)/10=40/10=4
((341 mod 100)-(341 mod 100))/100=(341-41)/100=300/100=3

That should work.

#### nemo

• LV9 Veteran (Next: 1337)
• Posts: 1203
• Rating: +95/-11
##### Re: Integer Extraction Problem
« Reply #2 on: July 09, 2010, 01:04:28 pm »
a simpler way to get the first digit in axe would be the following:
number /  (base ^ (digits in number - 1))

example: 341
341 / (10 ^ (3-1))
341 / 10^2
341 / 100
3.41
axe doesn't support floating point so....
3

if you're dealing with 3 digit numbers and above, graphmastur's algorithm is probably the best idea. if you're dealing with just two digits, the easier way would be to mod and divide the number by the base.

example: 34
34 ^ 10 = 4
34 / 10 = 3.4 = 3

#### _player1537

• Guest
##### Re: Integer Extraction Problem
« Reply #3 on: July 09, 2010, 01:14:33 pm »
or, (I don't think this was said) to get the 3rd number of a 5 digit number would be
number mod 1000 / 100
so for 12345 that would be
12345 mod 1000 = 345
345 / 100 = 3
« Last Edit: July 09, 2010, 01:14:41 pm by _player1537 »

#### jnesselr

• King Graphmastur
• LV11 Super Veteran (Next: 3000)
• Posts: 2270
• Rating: +81/-20
• TAO == epic
##### Re: Integer Extraction Problem
« Reply #4 on: July 09, 2010, 02:16:52 pm »
Oh, yes.  Sorry, didn't see it was axe, so you can get rid of the "-(num mod base^(n-1)" part, and it will still work.

#### Builderboy

• Physics Guru
• CoT Emeritus
• LV13 Extreme Addict (Next: 9001)
• Posts: 5673
• Rating: +613/-9
• Would you kindly?
##### Re: Integer Extraction Problem
« Reply #5 on: July 09, 2010, 02:29:16 pm »
Yeah the general formula to access a digit in base 10 is this.  To extract the Nth digit (starting from 0 on the right)

digit = #/(10^N) mod 10

#### Quigibo

• The Executioner
• CoT Emeritus
• LV11 Super Veteran (Next: 3000)
• Posts: 2031
• Rating: +1075/-24
##### Re: Integer Extraction Problem
« Reply #6 on: July 09, 2010, 02:31:15 pm »
The algorithm is like this to display a number:
Code: [Select]
.Z is the number you're trying to display!If Zsub(D,0)EndWhile Zsub(D,Z^10)Z/10->ZEnd
Where the subroutine D does something with the digit 0-9 and moves a cursor.  This displays the number from right to left and does not add extra zeros or spaces to the front of the number.
___Axe_Parser___
Today the calculator, tomorrow the world!

#### DJ Omnimaga

• Clacualters are teh gr33t
• CoT Emeritus
• LV15 Omnimagician (Next: --)
• Posts: 55937
• Rating: +3154/-232
• CodeWalrus founder & retired Omnimaga founder
##### Re: Integer Extraction Problem
« Reply #7 on: July 09, 2010, 07:50:25 pm »
(That stuff can be pretty useful when using custom fonts . I use something like this in Supersonic Ball for the timer at the bottom of the screen and will also use it when displaying the score)

The funny thing is that it seems faster to do all of this than just displaying the entire number at once using Text(0,0,Z>Dec (even if Text is set to not automatically update the LCD)
Dream of Omnimaga