format method
Format a number according to the settings of this number formatter instance.
Implementation
String format(dynamic number) {
if (number == null) {
return '';
}
if (number is! num && number is! String) {
throw ArgumentError(
'Invalid argument type. Expected num or String, got ${number.runtimeType}');
}
if (number is num && (number.isNaN || number.isInfinite)) {
return number.toString();
}
final double n =
number is String ? double.parse(number) : (number as num).toDouble();
if (_type == 'number') {
final String formatted =
(_style == 'scientific') ? _formatScientific(n) : _formatStandard(n);
if (n < 0) {
return _templateNegative.replaceAll('{n}', formatted);
}
return formatted;
} else {
// currency or percentage
final String formatted = _formatStandard(n);
final String template = (n < 0) ? _templateNegative : _template;
String result = template.replaceAll('{n}', formatted);
if (_type == 'currency' && _currencySign != null) {
result = result.replaceAll('{s}', _currencySign!);
}
return result;
}
}