This tutorial shows multiple ways to Convert Double to Int and Int to double in Dart and Flutter programming.
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 to convert from one type to other types.
How to convert Double to Int type in Dart programming?
There are multiple ways we can parse double to int in dart programming.
- 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 an double trucate() 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 lower of 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, int value directly assigned to double value, It gives 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 string version of an number, Next pass this string to double.parse(), assigned to double number Here is an example:
main() {
int number = 10;
double d1 = double.parse(number.toString());
print(d1); //10
}
- use divide operator with 1
This is simple and easy as all the division operation results in Double flutter.
Divide the integer number with 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.