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?
CodePudding user response:
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
}
CodePudding user response:
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]);
}
CodePudding user response:
void main() {
int num1 = 346775;
String num1s = num1.toString();
List num1l = num1s.split('');
print(num1l[1]);
}
