Dart Example to count the number of digits in a number| Decimals Count in Dart

This example covers two programs in dart.

  • the first program finds the count of several digits in an integer number

The length of a number is to find the count of the number of digits.

For example, 129 number digits count is 3.

  • The second program finds the count of a decimal of a double number For example, in the 129.1234 number, the decimal digit count is 4.

Consequently, Both are used to implement digit count for a given number.

How to get the count digit count of an integer number in dart and flutter

This program calculates the count of digits in a given number

  • First, the number is converted to a String using the toString() method.
  • Find the length of a string using the length method
  • Another way is using interpolation string with length method

Here is a program to find the number of digits counted in a given number

void main() {
  int number = 129;

  print(number.toString().length); //3
  print('$number'.length); //3
}

When you run the above program, the Output is

3
3

Count of decimal places in a given number in dart and flutter

This program counts the number of decimal digits in a given double number

  • Double number is created with decimal places
  • Convert, this number to a String using the toString() method
  • Next, Split the number using a dot(.) delimeter
  • Returns the decimals number as a string
  • Find the length of a string Following is an example of a function to count the number of decimal digits of a double number
void main() {
  double doubleNumber = 3.1435;
  var decimalsCount = doubleNumber.toString().split('.')[1];
  print(decimalsCount.length);
}

Output is

4

Conclusion

Learn how to calculate digit count in a given integer number in the dart program, Also find the fractional or decimal digits count in a double number.