formatDouble function

String formatDouble(
  1. dynamic number, {
  2. dynamic nullTxt = "-",
})

Implementation

String formatDouble(number, {nullTxt = "-"}) {
  if (number == null || number == "") {
    return nullTxt;
  }
  double num;
  try {
    num = double.parse(number.toString());
  } catch (e) {
    loggerNoStack.d("转换异常:$e");
    return nullTxt;
  }
  if ((num.toString().length - num.toString().lastIndexOf(".") - 1) < 2) {
    //小数点后有几位小数
    return num.toStringAsFixed(2)
        .substring(0, num.toString().lastIndexOf(".") + 2 + 1)
        .toString();
  } else {
    return num.toString()
        .substring(0, num.toString().lastIndexOf(".") + 2 + 1)
        .toString();
  }
}