0
How can I use the any digit of the int type number?
How do I take the number from any digit of an int number and use it as an int or useful other forms?
3 Antworten
+ 2
Specific digits of a number can be counted like this (index starts at 1 at the end of the number):
(n % 10 ^ i - n % 10 ^ i - 1...) / 10 ^ i - 1
(at the start, it ends when i reaches 0)
Example:
576
First digit (6), index 1:
(576 % 10) / 10 ^ 0 -> 6 / 1 -> 6
Second digit (7), index 2:
(576 % 100 - 576 % 10) / 10 ^ 1 -> (76 - 6) / 10 -> 70 / 10 -> 7
Or, you could turn it into a string and use the charAt method or if it's treated like an array, you could just get the index, that's why you should specify what language ;)
0
what language ? in python you can just do
n = 123456
print([int(i) for i in str(n)])
[1, 2, 3, 4, 5, 6]
0
C#