{

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


This tutorial shows multiple ways to Convert Double to Int and Int to double in Dart and Flutter programming. parse Double to int vice versa  in Dart or Flutter

Int is a primitive type that stores numeric numbers arbitrary-precision signed integer without decimals, Examples Int 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 manually converted from one type to another type.

How to convert Double to Int type in Dart programming?

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 int.

Syntax.

Int toInt()

Here is an 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, There are methods - round(), ceil(), floor() which will give rounded of 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 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

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: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example

How to round double numbers with limit the numbers with decimal places in Dart/ Flutter By Examples