doubleToString static method

String doubleToString(
  1. double source, {
  2. int fixed = 2,
  3. bool finishZero = false,
})

double转string

Implementation

static String doubleToString(double source, {int fixed = 2, bool finishZero = false}) {
  int fix = max(fixed, 0);
  if (fix == 0) {
    return source.truncate().toString();
  }
  if (finishZero) {
    return source.toStringAsFixed(fix);
  } else {
    int intValue = source.truncate();
    double decimalValue = source - intValue;
    if (decimalValue > 0) {
      String result = source.toStringAsFixed(max(fixed, 0));
      while (result.endsWith("0")) {
        result = result.substring(0, result.length - 1);
      }
      return result;
    } else {
      return intValue.toString();
    }
  }
}