getPrecisionsString static method

String getPrecisionsString({
  1. required double number,
  2. required Type type,
  3. required int digit,
  4. int value = 1,
})

Implementation

static String getPrecisionsString(
    {required double number,
    required Type type,
    required int digit,
    int value = 1}) {
  //保留整数情况下

  if (digit == 0) {
    if (type == Type.rounding) {
      //四舍五入
      return number.toStringAsFixed(0);
    } else if (type == Type.tailRemoval) {
      //去尾
      return number.toInt().toString();
    } else {
      String s = number.toStringAsFixed(2);
      s = s.substring(0, s.length - 1);
      String end = s.substring(s.length - 1, s.length);
      if (double.parse(end) > value) {
        return number.ceil().toString();
      } else {
        return number.toInt().toString();
      }
    }
  } else {
    //保留小数情况下
    if (type == Type.rounding) {
      //四舍五入
      type= Type.enter;
      value=5;
    }

    if (type == Type.tailRemoval) {
      //去尾
      String s = number.toStringAsFixed(digit + 1);
      return s = s.substring(0, s.length - 1);
    } else {
      //逢N进1
      String s = number.toStringAsFixed(digit + 2);
      s = s.substring(0, s.length - 1);
      String end = s.substring(s.length - 1, s.length);
      if (double.parse(end) >= value) {
        String a = s.substring(0, s.length - 1);
        String b = 0.toStringAsFixed(digit - 1);
        if(digit==1){
          b="0.";
        }
        b = b + "1";
        var c = Decimal.parse(a) + Decimal.parse(b);
        return c.toString();
      } else {
        s = s.substring(0, s.length - 1);
      }
      return s;
    }
  }
}