SOS video
SOS video

Reputation: 476

Get number of certain place of long number

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

Answers (3)

Yunus Kocatas
Yunus Kocatas

Reputation: 316

void main() {
  int num1 = 346775;
  String num1s = num1.toString();
  List num1l = num1s.split('');
  print(num1l[1]);
}

Upvotes: -1

Victor Eronmosele
Victor Eronmosele

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

julemand101
julemand101

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

Related Questions