i18n
i18n copied to clipboard
Add support for ordinals in `NumberFormat`
Is your feature request related to a problem? Please describe. I want to format a number with an ordinal suffix (e.g. "1st", "2nd", "3rd", "205th") in a localized way, as different languages have different ordinal suffixes.
Describe the solution you'd like
I would like a NumberFormat
that formats a number with an ordinal suffix, e.g.
factory NumberFormat.ordinal({String? locale});
, similar to NumberFormat.compact
.
Describe alternatives you've considered I'm currently using a custom solution, however this obviously only works in English and is not localized.
extension Ordinals on int {
String get ordinal {
switch (this % 10) {
case 1:
return '${this}st';
case 2:
return '${this}nd';
case 3:
return '${this}rd';
default:
return '${this}th';
}
}
}
I've also considered using native libraries (as all platforms supported by Flutter have native support for this), but this would mean that:
- I have to maintain a native library for this functionality
- It would likely mean that formatting the numbers needs to happen asynchronously (since we need to wait on a PlatformChannel) which is obviously not ideal if you just want to display a string.
Additional context
This issue is not a duplicate of https://github.com/dart-lang/i18n/issues/163, as this functionality does not inherently involve formatting dates, it's purely related to formatting numbers, and should therefore likely be an addition to NumberFormat
.