问题
I have double value
double myNum = 110700.00;
I want to modify it's format using NumberFormat
110 700
How it can be done?
回答1:
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));
}
回答2:
For int you can also use the String utils to add spaces in the required frequency. It seems though as you cannot choose to count the letters from the right, thus you need to reverse the String twice.
import 'package:basic_utils/basic_utils.dart';
String formatNumber(int number) {
var s = StringUtils.reverse(number.toString());
s = StringUtils.addCharAtPosition(s, " ", 3, repeat: true);
return StringUtils.reverse(s);
}
来源:https://stackoverflow.com/questions/50133321/change-grouping-separator-in-numberformat-for-dart