To use localized numbers in Flutter, you can use the NumberFormat
class from the intl package. This class provides methods for formatting and parsing numbers based on the user's locale.
To format a number in a specific locale, you can create an instance of the NumberFormat
class with the desired locale, and then use the format
method to format the number. Here's an example of formatting a number in English and German:
double number = 123456.789;
String formattedNumberEn = NumberFormat.decimalPattern('en').format(number);
print(formattedNumberEn); // 123,456.789
String formattedNumberDe = NumberFormat.decimalPattern('de').format(number);
print(formattedNumberDe); // 123.456,789
You can also format numbers in your localization messages using either the gen_l10n or Flutter Intl approaches and generated localization code will handle that for you. Here is an example of such a message in ARB file:
"pageHomeSampleTotalAmount": "Total: {total}",
"@pageHomeSampleTotalAmount": {
"placeholders": {
"total": {
"type": "double",
"format": "currency",
"optionalParameters": {
"symbol": "€",
"decimalDigits": 2
}
}
}
}