swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.41k stars 162 forks source link

No support for DescriptiveNumberFormatConfiguration.Presentation in IntegerFormatStyle or DecimalFormatStyle #1037

Open orj opened 3 weeks ago

orj commented 3 weeks ago

There currently appears to be a gap in the implementation of the FormatStyle APIs and foundation's NumberFormatter.

Although DescriptiveNumberFormatConfiguration.Presentation defines ordinal and spellOut there doesn't appear to be any presentation() APIs on IntegerFormatStyle or DecimalFormatStyle that take this type.

What this means is that if we want to format numbers 1, 2, 3, etc. in the style of "1st, 2nd, 3rd" etc we have to use NumberFormatter instead of [1, 2, 3].map { $0.formatted(.number.presentation(.ordinal)) }.

itingliu commented 2 weeks ago

It was intentional to leave them out for FormatStyle. Supporting ordinal and spell out numbers requires more context than what this API knows. For example, many languages have gendered ordinals, e.g. in Portuguese, "first" can be either "primeiro" or "primeira", and even in abbreviations ("1º", "1ª"). What we've seen in the past is that developers tend to use the formatted number as part of a localized string and caused grammatical errors. Not saying it's not doable, but it just requires more thought.