IGNORED

# Need help seperating 10s and 1s place into different variables

## Recommended Posts

Thank you in advance for any suggestions.  Having trouble wrestling with separating the tens part of a variable and the ones part into two different variables.

I'm trying to let players buy items.  I have a display that only has two digits.  I'd like to show that it takes, say, 87 gold to purchase.  So, the "8" part would be in one variable for display and the "7" part in another variable for display.

I've worked around a similar problem by manually adding nybbles.  But, I feel I'm doing it wrong.

If y'all have any experience with this kind of problem I'd love to hear it.

##### Share on other sites

I think I got it by hacking at the high score code from bogax:

``` f = 124

temp1 = f

rem JUST 1s PLACE
temp2= 0
if temp1 > 99 then temp2= temp2+ 0  : temp1 = temp1 - 100
if temp1 > 99 then temp2= temp2+ 0  : temp1 = temp1 - 100
if temp1 > 49 then temp2= temp2+ 0 : temp1 = temp1 - 50
if temp1 > 29 then temp2= temp2+ 0 : temp1 = temp1 - 30
if temp1 > 19 then temp2= temp2+ 0 : temp1 = temp1 - 20
if temp1 > 9 then temp2= temp2+ 0 : temp1 = temp1 - 10

c = temp1

temp1 = f

rem JUST 10s PLACE
temp2= 0
if temp1 > 99 then temp2= temp2+ 0 : temp1 = temp1 - 100
if temp1 > 99 then temp2= temp2+ 0 : temp1 = temp1 - 100
if temp1 > 49 then temp2= temp2+ 5 : temp1 = temp1 - 50
if temp1 > 29 then temp2= temp2+ 3 : temp1 = temp1 - 30
if temp1 > 19 then temp2= temp2+ 2 : temp1 = temp1 - 20
if temp1 > 9 then temp2= temp2+ 1 : temp1 = temp1 - 10

b = temp2```

##### Share on other sites

The first idea I had was using a variation of this:

```; Tens digit ........... _sc3 & \$F0 (00 00 X0)
; Ones digit ........... _sc3 & \$0F (00 00 0X)```

##### Share on other sites

1 hour ago, Random Terrain said:

The first idea I had was using a variation of this:

```; Tens digit ........... _sc3 & \$F0 (00 00 X0)
; Ones digit ........... _sc3 & \$0F (00 00 0X)```

I'll take a look.  Thanks for checking this topic (and others too!)

I think the butchered bogax high score code might be the most "optimal" since dividing by 10 is hard.

##### Share on other sites

What you want to do is subtract the 10's frome temp1 and add them to temp2 so you end up with the 10's in temp2 and the 1's in temp1

so the if statements would be like

if temp1 > 99 then temp2 = temp2 + 10 : temp1 = temp1 - 100     ; temp2 gets the 10's ie 100 = 10 10's

##### Share on other sites

12 hours ago, bogax said:

What you want to do is subtract the 10's frome temp1 and add them to temp2 so you end up with the 10's in temp2 and the 1's in temp1

so the if statements would be like

if temp1 > 99 then temp2 = temp2 + 10 : temp1 = temp1 - 100     ; temp2 gets the 10's ie 100 = 10 10's

Thanks again!  Yeah.  I decided to use your high score code including the 100s place so I could filter it out if I got a whacky condition like an unexpected number greater than 99.  I may have switched around the rem statements remarking which was dealing with 10s and 1s.

##### Share on other sites

I was thinking something like this

```  ;  d3 = 100's, d2 = 10's, d1 = 1's

d3 = 0 : d2 = 0 : d1 = temp1

if d1 >= 100 then d3 = d3 + 1 : d1 = d1 - 100
if d1 >= 100 then d3 = d3 + 1 : d1 = d1 - 100
if d1 >=  50 then d2 = d2 + 5 : d1 = d1 -  50
if d1 >=  30 then d2 = d2 + 3 : d1 = d1 -  30
if d1 >=  20 then d2 = d2 + 2 : d1 = d1 -  20
if d1 >=  10 then d2 = d2 + 1 : d1 = d1 -  10```

##### Share on other sites

Posted (edited)

Does bB have modulo and the integer division (basically, division where the decimal part is dropped, so with it 10 / 2 = 5, but also 11 / 2 = 5)? Because that would significantly make easier to split variable into 100s, 10s, and 1s.

//edit: If it does have both, the basic gist is this:

For your ones digit, it's value % 10.

For your tens digit, it's (value/10)%10.

For your hundreds digit, it's (value/100)%10

Where / is the integer division operator and % is modulo operator.

Edited by Darkhog
##### Share on other sites

15 hours ago, Darkhog said:

Does bB have modulo and the integer division (basically, division where the decimal part is dropped, so with it 10 / 2 = 5, but also 11 / 2 = 5)? Because that would significantly make easier to split variable into 100s, 10s, and 1s.

//edit: If it does have both, the basic gist is this:

For your ones digit, it's value % 10.

For your tens digit, it's (value/10)%10.

For your hundreds digit, it's (value/100)%10

Where / is the integer division operator and % is modulo operator.

bB does have integer division and a modulo operator. The double slash // is used for modulus. However, almost always you will want to find a way to avoid doing normal multiplication and division operations in a game, as these take up a whole lot of the very limited available cycles on the Atari. Multiplying and dividing by powers of two is an exception, since these translate to quick bit-shifting operations under the hood.

##### Share on other sites

From what it seems, the OP wants to create a shop system, so he/she doesn't need to do it every frame. Having separation done this way every so often (not every frame) wouldn't be that bad and it would improve readability. Heck, 1s could be separated on one frame, 10s on the next and so on, and the player wouldn't even notice.

Of course if it would to be done every frame, it would be bad for performance, but since it seems to be needed only every so often, readability of the code is more important.

##### Share on other sites

I needed to display a decimal time measurement using the score variables that natively work in hex.  Or, my hack of Karl Gs 4 scores mini kernel that instead displayed two large digits.  I'll definitely experiment with bogaxs posted code above.

It's also handy to use the score for debugging in-game.  So displaying decimal values is essential there too.  Here I'm using it to display sprite coordinates:

##### Share on other sites

Posted (edited)

Didn't RevEng jigger Bb to do BCD math

edit: dec

Edited by bogax

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.