Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency format (by locale) in Dart

I was able to get a number in currency format with the following:

final myLocale = Localizations.localeOf(context).toString();
final longNumberFormat = NumberFormat.currency(locale: myLocale, symbol: mySymbol, decimalDigits: 2);
print(longNumberFormat.format(1234));

And the result of this is:

for the locale 'en_US': $1,234.00

for the locale 'es' or 'es_AR': 1.234,00 $

In the first case (en_US) it is correct, but for the last case (es_AR) which is Argentina Spanish (my country) it is wrong, we don't use the symbol at the end, we use it in front like the US, but the dots/commas are correct.

This is a mistake of the library? Is there a work around for this?

Thanks

like image 948
Mark Watney Avatar asked Sep 05 '25 22:09

Mark Watney


1 Answers

As above, es_AR isn't in the data. You can't modify that file, as it's generated from CLDR data, and would get overwritten. But you can modify it at runtime to add a missing entry or modify an existing one. For example, here I've created an entry where I've taken the "es" entry and moved the currency symbol (\u00a4) to the beginning.

import 'package:intl/intl.dart';
import 'package:intl/number_symbols.dart';
import 'package:intl/number_symbols_data.dart';

main() {
  var argentina = NumberSymbols(
      NAME: "es_AR",
      DECIMAL_SEP: ',',
      GROUP_SEP: '.',
      PERCENT: '%',
      ZERO_DIGIT: '0',
      PLUS_SIGN: '+',
      MINUS_SIGN: '-',
      EXP_SYMBOL: 'E',
      PERMILL: '\u2030',
      INFINITY: '\u221E',
      NAN: 'NaN',
      DECIMAL_PATTERN: '#,##0.###',
      SCIENTIFIC_PATTERN: '#E0',
      PERCENT_PATTERN: '#,##0\u00A0%',
      CURRENCY_PATTERN: '\u00A4#,##0.00\u00A0',
      DEF_CURRENCY_CODE: r'$');

  numberFormatSymbols['es_AR'] = argentina;
  var f = NumberFormat.currency(locale: 'es_AR');
  print(f.format(1234));

}
like image 88
Alan Knight Avatar answered Sep 10 '25 06:09

Alan Knight