How can round this down in dart/flutter:

int hours = int.parse((minutes/60).toStringAsFixed(0));

minutes/60 is 7.92, and I want the result to be 7 but it gets rounded up to 8...


Solution 1: Alexandre Ardhuin

You can use both the Truncating division operator:

int hours = minutes ~/ 60;

Or the floor method:

int hours = (minutes/60).floor();


Solution 2: Олександр Бабіч

If you need to round down with certain precision - use this:

double roundDown(double value, int precision) {
    final isNegative = value.isNegative;
    final mod = pow(10.0, precision);
    final roundDown = (((value.abs() * mod).floor()) / mod);
    return isNegative ? -roundDown : roundDown;
  }

roundDown(0.99, 1) => 0.9

roundDown(-0.19, 1) => -0.1