formatCurrency function

String formatCurrency(
  1. String amount, {
  2. bool spaceIcon = true,
  3. bool ignoreSymbol = false,
  4. String symbol = '₦',
})

Implementation

String formatCurrency(
  String amount, {
  bool spaceIcon = true,
  bool ignoreSymbol = false,
  String symbol = '₦',
}) {
  final formatter = NumberFormat.currency(
    locale: "en_NG",
    name: ignoreSymbol ? '' : symbol, //?? '\$',
    symbol: ignoreSymbol ? "" : "$symbol${spaceIcon ? " " : ""}",
    decimalDigits: 2,
  );
  if (amount.isEmpty || amount == "null") {
    return "$symbol-.--";
  }
  amount = amount.replaceAll(RegExp(r'[^0-9\.]'), "");
  final amountDouble = double.tryParse(amount);
  if (amountDouble == null) {
    return amount;
  }
  if (amountDouble == 0) {
    return "${symbol}0.00";
  }
  return formatter.format(amountDouble);
}