how to convert double to int in dart?

Issue

I have a code like this

double total = 20.2;
int min = total as int;

flutter build was ok but it was exception on runtime.

I have tried int.parse() but it requires string not double.

Do you have any suggestion?

Solution

If you want to round the value :

double total = 20.2;
int min = total.round();

If you want to just truncate the value :

double total = 20.2;
int min = total.toInt();

Answered By – Mann Dave

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *