How to convert a double to an int in Dart?
Solution 1
Round it using the round()
method:
int calc_ranks(ranks) {
double multiplier = .5;
return (multiplier * ranks).round();
}
Solution 2
You can use any of the following.
double d = 20.5;
int i = d.toInt(); // i = 20
int i = d.round(); // i = 21
int i = d.ceil(); // i = 21
int i = d.floor(); // i = 20
Solution 3
You can simply use toInt() to convert a num
to an int
.
int calc_ranks(ranks)
{
double multiplier = .5;
return (multiplier * ranks).toInt();
}
Note that to do exactly the same thing you can use the Truncating division operator :
int calc_ranks(ranks) => ranks ~/ 2;
Solution 4
I see a lot of answers, but with less description. Hope my answer will add some value. Lets initalize the variable, and see how it will change with different methods.
double x = 8.5;
toInt()
It truncates the decimal value.
int a = x.toInt();
print(a); // 8
truncate()
It also truncates the decimal value.
int b = x.truncate();
print(b); // 8
round()
It returns the closest integer. It uses half up rounding mode.
int c = x.round();
print(c); // 9
ceil()
It returns the closest integer greater than the value.
int c = x.ceil();
print(c); // 9
floor()
It returns the closest integer smaller than the value.
int c = x.floor();
print(c); // 8
Solution 5
Dart round double to int
Using round()
method, we can get an integer closest to a double.
For example:
int num1 = (2.3).round();
// 2
int num2 = (2.5).round();
// 3
int num3 = (-2.3).round();
// -2
int num4 = (-2.5).round();
// -3
You can also try to those methods convert double to int in a Flutter
double x = 2.5;
int a = x.toInt();
int b = x.truncate();
int c = x.round();
int d = x.ceil();
int e = x.floor();
print(a); // 2
print(b); // 2
print(c); // 3
print(d); // 3
print(e); // 2
Related videos on Youtube

Comments
-
Todd Chambery 5 months
The following produces the below error:
int calc_ranks(ranks) { double multiplier = .5; return multiplier * ranks; }
The return type
double
is not aint
, as defined by the methodcalc_ranks
. How do I round/cast to anint
? -
Everton almost 9 yearsOr truncate it to int: return (multiplier * ranks).toInt(); (see api.dartlang.org/docs/channels/stable/latest/dart_core/…)
-
最白目 about 3 years@Evertion I had cases where
(0.57 * 100).toInt()
resolved to56
, so guys, be careful. -
Hector Aguero about 2 yearsSo, what is the difference between toInt and floor or round and ceil
-
iDecode about 2 yearsFirst, this question wasn't about what
round
means in Dart. Second, the rest of the things are only copied from the existing answer. Please don't spam SO like this. You're only COPYING other answers. -
Ruben about 2 years@iDecode Q: "How to convert a double to an int in Dart?" This Answer: "Using round() we can get an integer closest to a double". How come you think the answer is not related to the question?
-
iDecode about 2 years@Ruben This answer is already provided by other user. There's no benefit in posting same thing over and over again.
-
Paresh Mangukiya about 2 yearsI have already told you that from now on I will take care, even then why are you leaving a comment by down-voting. While the method is the same in this, but try to explain it differently, in this way I have given an example of how to take the number
-
Paresh Mangukiya about 2 yearsIn this I have tried to explain how an integer is closest to a double. by this int num1 = (2.3).round(); // 2 int num2 = (2.5).round(); // 3 int num3 = (-2.3).round(); // -2 int num4 = (-2.5).round(); // -3
-
Kamil Poniewierski over 1 yearFloor and ceil would convert the double to closest int, going down and up respectively, so (20.1).floor() would be 20, (20.1).ceil() would be 21. toInt cuts off the mantissa, leaving just the characteristic, and round makes it so the numbers with mantissa below 0.5 go down, others go up.