getPercentage static method

dynamic getPercentage(
  1. num? number,
  2. num? total, {
  3. bool toString = true,
  4. bool toDouble = false,
  5. bool toInt = false,
  6. int point = 2,
})

获取百分比

Implementation

static dynamic getPercentage(
  num? number,
  num? total, {
  bool toString = true,
  bool toDouble = false,
  bool toInt = false,
  int point = 2,
}) {
  if (number != null && total != null) {
    final n = double.parse(number.toString());
    final t = double.parse(total.toString());
    String percentStr = '0';
    if (t != 0) {
      percentStr = ((n * 100) / t).toStringAsFixed(point);
    }
    if (toDouble) {
      return double.parse(percentStr);
    } else if (toInt) {
      return int.parse(percentStr);
    } else {
      return percentStr;
    }
  } else {
    if (toDouble) {
      return 0.0;
    } else if (toInt) {
      return 0;
    } else {
      return '0';
    }
  }
}