{

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


parse Double to int vice versa  in Dart or Flutter

This tutorial shows multiple ways to Convert Double to Int and Int to double in Dart and Flutter programming.

  • Convert Double to Int in Dart:

    • use double.toInt() method, For example: 3.4.toInt() returns int value 3.
    • double.truncate() method to remove decimals from a number.3.4.truncate() returns int value 3.
    • rounding methods such as round, ceil, and floor based on lower or upper value of a given number.
  • Convert Int to Double in Dart:

    • Int.toDouble() returns double value
    • double.parse(numberstring) method that takes number string, returns double, Example double.parse("123") returns double 123
    • use division(/) operator to divide number by 1.

let’s see what are Int and Double types in Dart and Flutter.

Int is a primitive type that stores numeric numbers arbitrary-precision signed integer without decimals, Examples 1 types values are 1,5.

Double is a primitive type that stores IEEE-754 double-precision floating numbers. Example double type values are 1.34,1.0.

Int and Double are different types and hold different values. Automatic conversion does not happen in the Dart compiler.

We have to convert from one type to another type manually

How to convert Double to Int type in Dart?

There are multiple ways we can parse double to int in dart programming.

  • use Double toInt() method double.toInt() method removes the decimals from a given double number and returns the int value.

Syntax.

Int toInt()

Here is a Double toInt() Example

main() {
  double double1 = 3.4;
  print(double1.runtimeType); // double
  int i1 = double1.toInt();
  print(i1); // 3
  print(i1.runtimeType); // int
}
  • Double truncate() method

double.truncate() method also ignores the decimal value and returns the int value.

Syntax:

int truncate()

Here is a double truncate() example program

main() {
  double double1 = 12.9;
  print(double1.runtimeType); // Double
  int i1 = double1.truncate();
  print(i1); // 12
  print(i1.runtimeType); // int
}

Another way, using methods - round(), ceil(), floor() which does round a given double number in dart and flutter.

  • rounded a numbers using round(),ceil(), floor()

    • round() method: returns the closest int number. For example, 6.5.round() returns 7.

      Here is a round() method example program

    main() {
  double double1 = 12.9;
  double double2 = 3.4;

  print(double1.runtimeType); // double
  print(double2.runtimeType); // double

  int i1 = double1.round();
  print(i1); // 13
  print(i1.runtimeType); // int
  int i2 = double2.round();
  print(i2); // 3
  print(i2.runtimeType); // int
}
  • ceil() method: returns the ceiling number which is upper of a given int part. For example, 9.5.round() returns 10.

Here is a ceil() method example program

    main() {
  double double1 = 12.9;
  double double2 = 3.4;

  print(double1.runtimeType); // double
  print(double2.runtimeType); // double

  int i1 = double1.ceil();
  print(i1); // 13
  print(i1.runtimeType); // int
  int i2 = double2.ceil();
  print(i2); // 4
  print(i2.runtimeType); // int
}
  • floor() method : returns the rounded floor number which is lower than a given int part. For example, 9.5.round() returns 9.

Here is a floor() method example program

main() {
  double double1 = 12.9;
  double double2 = 3.4;

  print(double1.runtimeType); // double
  print(double2.runtimeType); // double

  int i1 = double1.floor();
  print(i1); // 12
  print(i1.runtimeType); // int
  int i2 = double2.floor();
  print(i2); // 3
  print(i2.runtimeType); // int
}

All the above methods work in flutter programming to convert double to int.

How to parse int to double in Dart or Flutter?

For example, an int value directly assigned to a double value gives the error A value of type ‘int’ can’t be assigned to a variable of type ‘double’. for a below code.

  int number = 10;
  double dd=number;

Following are multiple ways to convert int type to double in flutter or dart.

  • use int toDouble method

toDouble()method of int can be assigned to double type. Here is an example:

main() {
  int number = 10;
  double d1 = number.toDouble();
  print(d1); //10
  print(d1.runtimeType); //10
}
  • double parse method

First, get the string version of a number, Next, pass this string to double.parse(), assigned to a double number. Here is an example:

main() {
  int number = 10;
  double d1 = double.parse(number.toString());
  print(d1); //10
}
  • use the divide operator with 1

This is simple and easy as all the division operation results in Double flutter.

Divide the integer number by 1 results in a double type value. Here is an example:

main() {
  int number = 10;
  double d1 = number/1;
  print(d1); //10
}

Conclusion

To summarize, We can easily convert numeric types with built-in methods provided by int and double.

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

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

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

Dart Example - Program to check Leap year or not

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