
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:
- First Way, use
double.toInt()
method, For example:3.4.toInt()
returnsint
value3
. - Second way, use
double.truncate()
method to remove decimals from a number.3.4.truncate()
returnsint
value3
. - Third way, use rounding methods such as
round
,ceil
, andfloor
based on lower or upper value of a given number.
- First Way, use
Convert Int to Double in Dart:
Int.toDouble()
returnsdouble
valuedouble.parse(numberstring)
method that takes number string, returnsdouble
, Exampledouble.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 contains number with decimals.
double.toInt()
method removes the decimals from a given double number and returns the int
value.
Syntax.
Int toInt()
Returns integer number.
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.