mx_moneyFmt method

String mx_moneyFmt({
  1. int digits = 2,
  2. bool hasPlus = false,
})

Implementation

String mx_moneyFmt({int digits = 2, bool hasPlus = false}) {
  var splitText = (digits > 0 ? '.' : '');
  if (this == null) return '0' + splitText + '0' * digits;
  var result = "${this!.toDouble().toStringAsFixed(digits)}";
  if (digits == 2) {
    if (this.toString().isInteger || result.endsWith('00')) {
      return this?.toInt().mxText ?? '0';
    }
    result = "${this!.toDouble().toStringAsFixed(digits + 1)}";
    return result.substring(0, result.length - 1);
  }
  if (hasPlus) return "${this! > 0 ? '+' : '-'}${result.replaceAll('-', '')}";
  return result;
}