format method
Formats the decimal amount
using the currency object attached to the
MoneyFormatter instance and returns the textual representation or '' if
the amount is unavailable.
amount
for which a textual representation is returned.
symbols
specify true to prepend any symbol prefix or append any symbol
suffix as specified by the currency object's symbolPrefix and
symbolSuffix properties.
grouping
specify true to separate groups of digits using ',' character.
negative
specify true to prepend a negative sign if the amount is
negative.
Implementation
String format(Decimal? amount,
{bool symbols = true, bool grouping = true, bool negative = true}) {
if (amount == null) {
return '';
}
var s = amount.toStringAsFixed(_currency.exponent);
var amountNegative = s.startsWith('-');
if (grouping) {
var whole = amountNegative ? s.substring(1) : s;
var fractional = '';
if (_currency.exponent > 0) {
fractional = whole.substring(whole.length - _currency.exponent - 1);
whole = whole.substring(0, whole.length - (_currency.exponent + 1));
}
if (whole.length > 3) {
var buf = StringBuffer();
var i = whole.length - 1;
var count = 1;
while (i >= 0) {
var c = whole[i];
buf.write(c);
if (count % 3 == 0 && i != 0) {
buf.write(',');
}
i--;
count++;
}
whole = buf.toString().reverse();
}
s = (amountNegative ? '-' : '') + whole + fractional;
}
if (symbols) {
s = s.insert(_currency.symbolPrefix, amountNegative ? 1 : 0);
s = s + _currency.symbolSuffix;
}
if (!negative) {
s = s.removePrefix('-');
}
return s;
}