Dart - round down a double

Viewed 11503

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...

2 Answers

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

Related