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.
- Convert Double to Int in Dart:
- First Way, use
double.toInt()method, For example:3.4.toInt()returnsintvalue3. - Second way, use
double.truncate()method to remove decimals from a number.3.4.truncate()returnsintvalue3. The third way, use rounding methods such asround,ceil, andfloorbased on the lower or upper value of a given number.
- First Way, use
- Convert Int to Double in Dart:
Int.toDouble()returnsdoublevaluedouble.parse(numberstring)method that takes number string, returnsdouble, Exampledouble.parse("123")returns double 123- use
division(/) operator to divide number by 1.
Let’s explore the Int and Double types in Dart and Flutter.
Int is a primitive type designed to store numeric values, specifically, arbitrary-precision signed integers without decimals. Examples of int type values include 1 and 5.
On the other hand, Double is a primitive type tailored to store IEEE-754 double-precision floating-point numbers. Examples of double-type values encompass 1.34 and 1.0.
It’s crucial to note that Int and Double are distinct types that hold different kinds of values. The Dart compiler does not automatically convert between them; therefore, manual conversion is required when converting from one type to another.
How to convert Double to Int type in Dart?
There are various methods to convert a double to an int in Dart programming.
use Double toInt() method:
A Double type in Dart is designed to store numbers with decimals.
The
double.toInt()method is utilized to eliminate decimals from a given double number, returning the correspondingintvalue.Syntax.
Int toInt()Returns integer number.
Here is a
Double toInt() Examplemain() { 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 theintvalue.Syntax:
int truncate()Here is a
double truncate() example programmain() { 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 givendoublenumber in dart and flutter.rounded a number using
round(),ceil(), andfloor()round() method: returns the closest int number. For example, 6.5.round() returns 7.Here is a
round() method example programmain() { 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 the 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, obtain the string representation of a number. Next, pass this string to
double.parse(), and assign the resulting value to a variable of typedouble.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 operations result in Double flutter.
Divide the integer number by 1 resulting 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.
