0

i'd made this currency pattern to prefix a sign (+ or -) based on the value

static final String _currencyWithPrefixSignAndSymbol = "+ \u00A4 0.00 ;- \u00A4 0.00";

the pattern put a + prefix if the value is positive and a - if negative;

Problem: i need to remove these signs when the value is 0, is there a way to do this in the pattern ? without doing something like a string.Replace at the final?

Gabriel Guedes
  • 481
  • 5
  • 15

1 Answers1

2

As an option.
Since Dart 2.7 you can create extensions. So you can create num extension like this:

extension MyCurrencyFormat on num {
  static final _currencyWithPrefixSignAndSymbol = NumberFormat("+ \u00A4 0.00;- \u00A4 0.00");
  static final _currencyZero = NumberFormat("  \u00A4 0.00");

  String toCurrencyFormat() {
    return this == 0 ? _currencyZero.format(this) : _currencyWithPrefixSignAndSymbol.format(this);
  }
}

and then use it in code in that way:

var str1 = 42.toCurrencyFormat(); //  + USD 42.00
var str2 = 0.toCurrencyFormat();  //  USD 0.00