飞镖-四舍五入

时间:2019-06-18 19:32:34

标签: flutter dart

如何在dart / flutter中对此进行四舍五入:

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

分钟/ 60 7.92 ,我希望结果为 7 但会四舍五入到八...

2 个答案:

答案 0 :(得分:3)

您可以同时使用Truncating division operator

int hours = minutes ~/ 60;

floor method

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

答案 1 :(得分:0)

如果您需要以一定的精度舍入-使用此:

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