Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number's length in Dart?

Tags:

flutter

dart

How could I get the length of a number in Dart? Is there an equivalent to the one used in strings?

For ex: 1050000 has a length of 7. How could I dynamically discover this?

like image 441
Rodrigo Vieira Avatar asked Apr 07 '19 04:04

Rodrigo Vieira


People also ask

How do you find the length of a Dart number?

To find a length of a string in the dart, we make use of length property of a string class in Dart. This method returns the length of string which in integer.

How do you measure the length of a string in darts?

To find length of a given string in Dart, you can use length property of String class. String. length returns an integer specifying the number of characters in the string or otherwise called length.

How do you find the length of a List in flutter?

To get the length of a List in Dart, read its length property. length property is read-only for fixed length lists and writable for growable lists.

How do you get a substring in darts?

To find the substring of a string in Dart, call substring() method on the String and pass the starting position and ending position of the substring in this string, as arguments.


2 Answers

You can try this.

int i = 1050000;

int length = i.toString().length; // 7
// or
int length = '$i'.length; // 7
like image 139
CopsOnRoad Avatar answered Oct 09 '22 15:10

CopsOnRoad


With this extension method you would be able to use the length method on any number.

extension Num on num {
  int length() => this.toString().length;
}
like image 40
luisredondo Avatar answered Oct 09 '22 14:10

luisredondo