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?

12

Best Answer


Round it using the round() method:

int calc_ranks(ranks) {double multiplier = .5;return (multiplier * ranks).round();}

In Flutter, you can convert a double to an int using the toInt() method. This method truncates the decimal part of the double and returns the integer value. It is important to note that the conversion may result in data loss, as the decimal part will be discarded.

To convert a double to an int, simply call the toInt() method on the double variable. For example, if you have a double variable called 'myDouble', you can convert it to an int using 'int myInt = myDouble.toInt();'. This will assign the truncated integer value to the 'myInt' variable.

It is advisable to check if the double value can be accurately represented as an int before performing the conversion. To do this, you can use the isFinite property. For instance, you can check if 'myDouble.isFinite' is true before calling 'myDouble.toInt();' to ensure that the conversion will not result in an overflow or underflow error.

Keep in mind that when converting a double to an int, rounding does not occur. The decimal part is simply discarded. If you need to round a double to the nearest integer, you can use the round() method instead. This method rounds the double to the nearest integer value, following the standard rounding rules. For example, if you have a double variable called 'myDouble', you can round it to the nearest integer using 'int myInt = myDouble.round();'.

You can use any of the following.

double d = 20.5;int i = d.toInt(); // i = 20int i = d.round(); // i = 21int i = d.ceil(); // i = 21int i = d.floor(); // i = 20

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;

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

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.

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

Its easy,

(20.8).round()

For String,

double.tryParse(20.8).round()

from string to int ->

  1. 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()

  2. convert double to int

    doubleValue.toInt()

Try this!

int calc_ranks(ranks){double multiplier = .5;return (multiplier * ranks).truncate();}
 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,), 

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())