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?
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();
Try below code
Using toInt()
Using truncate()
void main() {
double d = 20.2;
//using toInt();
int a = d.toInt();
print('Using toInt - $a');
// using truncate();
int b = d.truncate();
print('Using truncate - $b');
}
Possible duplicate with How to convert a double to an int in Dart?
In short, you can do that using .toInt()
double total = 20.2;
print(total.toInt());