Search code examples
flutterlocalecurrencynumber-formatting

Flutter NumberFormat format without changing language


I am trying to format the number according to selected currency without translating it. For example the

Text(NumberFormat.simpleCurrency(
    locale: 'bn_BN',
).format(123456.78));

outputs

১,২৩,৪৫৬.৭৮৳

I want the format but not on the locales language. How do i get it to look like this instead -

1,23,456.78 BDT or 1,23,456.78 ৳

Is there a way to achieve this

P.S. I just need the thousands seperator or grouping to be what the currency default is for example BDT grouping is XX,XX,XXX first comma after thousand then comma after each 2 digits. This is something the NumberFormat does on its own but unfortunately only when u pass in locale.


Solution

  • You can define a custom NumberFormatter somewhere in your code (assuming that you will use it in different parts of your application) and format() numbers using it:

    final myFormatter = new NumberFormat.currency(
          name: 'BDT',
          decimalDigits: 2,
          customPattern: '#,##,##0.00 \u00A4',
        );
    
    Text(myFormatter.format(12345678.01)),
    

    This will result in: 1,23,45,678.01 BDT