{

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


Dart count of digits of a number and decimal numbers

This example covers two programs in dart.

  • the first program finds the count of a number of 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, 129.1234 number, 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 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 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 for 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example