Computer Science Canada Splitting up digits into var |
Author: | Freakish [ Mon Sep 11, 2006 9:45 pm ] |
Post subject: | Splitting up digits into var |
How can I split up digits into var Ex. I type in the number 392175 var digit01 is = to 3 var digit02 is = to 9 var digit03 is = to 2 var digit04 is = to 1 var digit05 is = to 7 var digit06 is = to 5 |
Author: | ericfourfour [ Mon Sep 11, 2006 10:22 pm ] |
Post subject: | |
I guess you could divide the number by a power of ten. Then, you could truncate the decimal. |
Author: | TokenHerbz [ Tue Sep 12, 2006 2:31 am ] | ||
Post subject: | |||
that, or the long way ![]()
|
Author: | zylum [ Tue Sep 12, 2006 7:26 am ] | ||
Post subject: | |||
or you could do it the cool way ![]()
|
Author: | TokenHerbz [ Tue Sep 12, 2006 10:44 am ] |
Post subject: | |
zylum, why did you use "ceil" when your not dealing with decimals? |
Author: | do_pete [ Tue Sep 12, 2006 10:51 am ] | ||
Post subject: | |||
Or you could do it the easy and short way:
|
Author: | Cervantes [ Tue Sep 12, 2006 1:39 pm ] |
Post subject: | |
Short, maybe, but not easy. At least not for the computer. Does it really make sense to turn a number into a string, split it up, then turn it back? It makes far more sense to do it the math way. |
Author: | zylum [ Tue Sep 12, 2006 3:27 pm ] |
Post subject: | |
TokenHerbz wrote: zylum, why did you use "ceil" when your not dealing with decimals?
where do you not see decimals? i used 2 lns and a divide... looks like plenty of decimals to me ![]() |
Author: | richcash [ Tue Sep 12, 2006 8:57 pm ] | ||
Post subject: | |||
Quote: zylum, why did you use "ceil" when your not dealing with decimals?
It's good that you asked, beacuse I say that one should never use an algorithm unless they know fully what it means. I get the feeling that some people won't know what zylum is doing giving such a complex upper bound to his array. Well, maybe it would help if you thought of it like this :
The upper bound of this array is equal to the number of digits in number. Now you can see it more easily, if you know what LOG is. |