Reputation: 476
How can I get a number of a certain place, for example, when I have a number like 12345, I need the number on the second place, so in this case the 2. A few more examples that you get the idea:
346775 => 4
673456 => 7
099784 => 9
How is this possible in Dart/Flutter?
Upvotes: 0
Views: 720
Reputation: 316
void main() {
int num1 = 346775;
String num1s = num1.toString();
List num1l = num1s.split('');
print(num1l[1]);
}
Upvotes: -1
Reputation: 7706
You can convert the number to a String
and get the element at index 1
from the String
.
void main() {
final int longNumber = 12345;
final int numberAtSecondPlace = int.parse(longNumber.toString()[1]);
print(numberAtSecondPlace); //2
}
Upvotes: 3
Reputation: 31219
Not saying this is the most efficient way to do it. But it is the easiest way I can come up with right now.
void main() {
print(346775.digitAt(1)); // 4
print(673456.digitAt(1)); // 7
print(099784.digitAt(1)); // 9
}
extension DigitAtOnInt on int {
int digitAt(int index) => int.parse(toString()[index]);
}
Upvotes: 2