Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change grouping separator in NumberFormat for Dart

I have double value

double myNum = 110700.00;

I want to modify it's format using NumberFormat

110 700

How it can be done?

like image 576
moonvader Avatar asked May 02 '18 11:05

moonvader


1 Answers

You can't do it without changing locale because GROUP_SEP is final.

However, if you don't mind changing locale, which you can do on any particular instance, for example with new NumberFormat('###,000', 'fr') then pick any locale (e.g. French) which uses non-breaking space as the GROUP_SEP. Of course, you then end up with , as your decimal separator but if you don't ever use it then it's moot. That happens to work for the example in the question, but doesn't generalize.

It's possible (though fragile) to define your own language. So if you happen to be an English-speaking Australian who prefers non-breaking space as your group separator then define your own locale (e.g. zz)

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

    main() {
      numberFormatSymbols['zz'] = new NumberSymbols(
        NAME: "zz",
        DECIMAL_SEP: '.',
        GROUP_SEP: '\u00A0',
        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%',
        CURRENCY_PATTERN: '\u00A4#,##0.00',
        DEF_CURRENCY_CODE: 'AUD',
      );

      print(new NumberFormat('###,000', 'zz').format(110700));
    }
like image 124
Richard Heap Avatar answered Nov 19 '22 13:11

Richard Heap