2

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?

3 Answers3

4

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();
Mann Dave
  • 68
  • 4
0

Try below code

  1. Using toInt()

  2. 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');
     }
    
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40
0

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());
gie3d
  • 766
  • 4
  • 8