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?
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.
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.
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.
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.
You can try this.
int i = 1050000;
int length = i.toString().length; // 7
// or
int length = '$i'.length; // 7
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With