Reputation: 3455
The following produces the below error:
int calc_ranks(ranks)
{
double multiplier = .5;
return multiplier * ranks;
}
The return type double
is not a int
, as defined by the method calc_ranks
. How do I round/cast to an int
?
Upvotes: 234
Views: 271118
Reputation: 267374
You can use any of the following.
double d = 20.5;
int i = d.truncate(); // i = 20; discards any fractional digits
int i = d.toInt(); // i = 20; truncates to an integer, same as truncate()
int i = d.round(); // i = 21; closest integer to d
int i = d.ceil(); // i = 21; least integer no smaller than d
int i = d.floor(); // i = 20; greatest integer no greater than d
Upvotes: 209
Reputation: 653
class CurrencyUtils{
static int doubletoint(double doublee) {
double multiplier = .5;
return (multiplier * doublee).round();
}
}
----------------------
CustomText( CurrencyUtils.doubletoint(
double.parse(projPageListss[0].budget.toString())
).toString(),
fontSize: 20,
color: Colors.white,
font: Font.QuicksandSemiBold,
),
Upvotes: 0
Reputation: 93
To convert double to int just this: division
double01 ~/ double02
PS: The operator x ~/ y is more efficient than (x / y).toInt().
Multiplication, addition and subtraction
(double01 * double02).toInt
(double01 + double02).toInt
(double01 - double02).toInt
Upvotes: 5
Reputation: 219
I looked at the answers above and added some more answers to make it a little easier to understand.
double value = 10.5;
Using toInt()
void main(){
double value = 10.5;
var y = value.toInt();
print(y);
print(y.runtimeType);
}
Using round()
The round() method returns the closest integer to the double.
void main(){
double value = 9.6;
var b = value.round();
print(b);
print(b.runtimeType);
}
Using ceil()
The ceil() method returns the smallest integer that is equal or greater than the given double.
void main(){
double value = 9.5;
var d = value.ceil();
print(d);
print(d.runtimeType);
}
Using floor()
The floor() method returns the greatest integer not greater than the given double.
void main(){
double value = 10.9;
var j = value.floor();
print(j);
print(j.runtimeType);
}
Conclusion
We’ve gone through 4 different techniques to convert a double to an integer in Dart and Flutter. You can choose from the method that fits your use case to solve your problem. Flutter is awesome and provides a lot of amazing features.
Upvotes: 7
Reputation: 34160
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
Upvotes: 25
Reputation: 76183
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;
Upvotes: 92
Reputation: 3
Try this!
int calc_ranks(ranks)
{
double multiplier = .5;
return (multiplier * ranks).truncate();
}
Upvotes: 0
Reputation: 911
There's another alternative, you can first cast the double to 'num' datatype and then convert to int using toInt().
double multiplier = .5;
return ((multiplier * ranks) as num).toInt();
The num type is an inherited data type of the int and double types. You can cast both int and double to num, then cast it again to whatever you want
(double -> use toDouble(), int -> use toInt())
Upvotes: -1
Reputation: 4239
from string to int ->
if you string in int format like '10' then use ---->
int.parse(value)
but if string in double format like '10.6' then use like this ---->
double.parse(value).toInt()
convert double to int
doubleValue.toInt()
Upvotes: 1
Reputation:
Round it using the round()
method:
int calc_ranks(ranks) {
double multiplier = .5;
return (multiplier * ranks).round();
}
Upvotes: 281