formatNumber static method

String? formatNumber(
  1. dynamic number, {
  2. bool showComma = true,
  3. int decimals = 2,
  4. bool fixedDecimal = true,
})

Implementation

static String? formatNumber(dynamic number, {bool showComma = true, int decimals = 2, bool fixedDecimal = true}){
  if(number == null) return null;

  var frontFormat = "#,##0";
  if(!showComma) frontFormat = "0";

  var decimalFormat = "";
  for(int i = 0; i< decimals; i++){
    if(decimalFormat.isEmpty){
      decimalFormat = ".";
    }
    decimalFormat = decimalFormat + (fixedDecimal != true ? "#" : "0");
  }
  final format = frontFormat + decimalFormat;
  return NumberFormat(format).format(number);
}