ID:165875
 
Is there a way to break down a 4-digit number into 4 separate digits?

ie. turn var/something=1746 into
var/somea=1
var/someb=7
var/somec=4
var/somed=6

Thanks.
Sure there's a way.

proc
breakup(n,numdigits) //Breaks up n into 'numdigits' digits, returns them in a list
if(!n||!isnum(n)) return null
var
list/digits=list()
keepgoing=1
k=0
for(var/i=1, i<=numdigits, i++)
k=round(10*(n-round(n*(0.1**i)))) //This bit is the heart of it. What we're doing is getting the digit we want just right of the decimal place, then cutting off everything over the decimal point. Then, we're putting the digit we want just to the right of the decimal point, and cutting off everything to the right.
digits+=k
return digits


There may be something better, I just hacked that up then. :P
In response to Jp
That lost me.
        setmoney(n as num)
moneycheck(n,4)
proc
moneycheck(n,numdigits)
if(!n||!isnum(n)) return null
var
list/digits=list()
keepgoing=1
k=0
for(var/i=1, i<=numdigits, i++)
k=round(10*(n-round(n*(0.1**i))))
digits+=k
src.ru1.icon_state="[src.r1]"
src.ru2.icon_state="[src.r2]"
src.ru3.icon_state="[src.r3]"
src.ru4.icon_state="[src.r4]"
src.money=((src.ru1*1000)+(src.ru2*100)+(src.ru3*10)+src.ru4)

Eh?

Before that it was:
        setmoney(n as num)
moneycheck(n,4)
proc
moneycheck()
src.ru1.icon_state="[src.r1]"
src.ru2.icon_state="[src.r2]"
src.ru3.icon_state="[src.r3]"
src.ru4.icon_state="[src.r4]"
src.money=((src.ru1*1000)+(src.ru2*100)+(src.ru3*10)+src.ru4)


It's been about 15 hours..
In response to FireEmblem
FireEmblem wrote:
It's been about 15 hours..

So it has and why are you bumping the topic..

People have better things to do than refreshing byonds forum page to answer questions just be patient
In response to A.T.H.K
I know that, I've been here long enough to know something as simple as that, but it was on page 2, so I was just bumping it.
In response to FireEmblem
The rules are actually that you have to wait till it's off the front page AND it's been 24 hours.

As for your problem, Jp's proc returns a list of digits. You're not meant to combine it into your moneycheck() proc, you're meant to create a list in your moneycheck() proc and have that list equal to Jp's proc like so:

var/list/digits = breakup(n,digits)


And use that list in order to do what ever it is you want to.
In response to DeathAwaitsU
mob
verb
setmoney(n as num)
usr.rupees=n
var/list/digits = breakup(n,4)
usr.ru1.icon_state="[digits[1]]"
usr.ru2.icon_state="[digits[2]]"
usr.ru3.icon_state="[digits[3]]"
usr.ru4.icon_state="[digits[4]]"

That gave me the runtime error:
runtime error: list index out of bounds
proc name: setmoney (/mob/verb/setmoney)
usr: FireEmblem (/mob)
src: FireEmblem (/mob)
call stack:
FireEmblem (/mob): setmoney(4256)
In response to FireEmblem
I think Jp's proc doesn't work. Here's something I quickly wrote up:

proc
get_digits(n)
if(isnum(n))
var/num = num2text(n,99999999) //convert the number to text
//length() of the variable will tell us how many digits there are in the number
//and we can copytext() parts of the number out
var/list/digits = list()
for(var/i=1,i<=length(num),i++) //this loop will let us copytext() out each digit
//you start at the first digit and work your way up till the last digit, which occurs at length(num)
digits += text2num(copytext(num,i,i+1))
return digits


Then applying this to your verb would be:

mob
verb
setmoney(n as num)
usr.rupees=n
var/list/digits = get_digits(n)
if(digits) //make sure get_digits() worked
usr.ru1.icon_state="[digits[1]]"
usr.ru2.icon_state="[digits[2]]"
usr.ru3.icon_state="[digits[3]]"
usr.ru4.icon_state="[digits[4]]"


*Edit*

Changed the num2text() SigFig argument a bit.

Also, I just realised this won't work at all for non-integer numbers or for negative numbers.

For negative numbers, I suggest editing get_digits() to check if the number is less than 0. If it is, abs() the whole number and do everything as normal. But before returning the list, make the first element in the list negative. But you may want to make everything in the list negative - it really depends on how you want to do it.

For non-integer numbers, work out where the decimal point is. After doing so, multiply the number by 10^n, where n represents where the decimal point is, and then use get_digits() on the new number. So if your number was 6345.6778, you'd want to multiply it by 10^4 which would result in 63,456,778.

*Edit 2*

If you want some help on working out where the decimal point is, num2text() the number first. Then use findtext() to locate the decimal point, which would just be ".".

*Edit 3*

I think I'll just point this out before someone comments on it.

mob
verb
setmoney(n as num)
usr.rupees=n
var/list/digits = get_digits(n)
if(digits) //make sure get_digits() worked
usr.ru1.icon_state="[digits[1]]"
usr.ru2.icon_state="[digits[2]]"
usr.ru3.icon_state="[digits[3]]"
usr.ru4.icon_state="[digits[4]]"


That will give you a "list index is out of bounds" runtime error if the digits list isn't long enough (which means the number you put in was less than 1000, assuming we're dealing with positive real integer numbers). I'll leave it up to you to figure out how to modify usr.ru based on digits.len.
In response to DeathAwaitsU
Thanks! One thing though, do I need to make a new proc and verb for each amount of money (there won't actually be verbs to gain money, just testing) for different numbers of digits? For example, I can't enter in 999, 99 or 9.
In response to FireEmblem
FireEmblem wrote:
Thanks!

You're welcome.

One thing though, do I need to make a new proc and verb for each amount of money (there won't actually be verbs to gain money, just testing) for different numbers of digits? For example, I can't enter in 999, 99 or 9.

I don't get what you mean. get_digits() should return individual digits for 999, 99 and 9. You don't need to tell it how many digits your number has.
In response to DeathAwaitsU
Oh wait. I see how to fix this. Nevermind. Thanks again.
In response to DeathAwaitsU
.len? Wha? I was just thinking that I'd do a check to see if it's a 3 digit number, 2 digit number, etc with if()s and then choose to exclude ru4 if it's 3 digits, ru3 if it's 2 digits, etc..
In response to FireEmblem
You could do that, but there are other less if()fy ways.