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...
You can use both the Truncating division operator:
int hours = minutes ~/ 60;
Or the floor method:
int hours = (minutes/60).floor();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With