Search code examples
dartformattingcurrency

Dart: Get the currency code given a locale


Specific Dart question. Given a locale (i.e. 'en_US', 'es_ES', ...), how can I get the associated currency symbol?

Do you know if there's a library similar to Currency in Java? https://docs.oracle.com/javase/8/docs/api/java/util/Currency.html


Solution

  • You can use the intl package (https://pub.dartlang.org/packages/intl)

    import 'package:intl/intl.dart';
    
    main() {
      var formatEnUs = NumberFormat.simpleCurrency(locale: 'en_US');
      print(formatEnUs.currencySymbol); // $
      print(formatEnUs.currencyName); // USD
    
      var formatEs = NumberFormat.simpleCurrency(locale: 'es_ES');
      print(formatEs.currencySymbol); // €
      print(formatEs.currencyName); // EUR
    }