NightOwl888 / ICU4N

International Components for Unicode for .NET
Apache License 2.0
27 stars 7 forks source link

BREAKING: Added Rule-Based Number Format Static API #46

Closed NightOwl888 closed 1 year ago

NightOwl888 commented 1 year ago

Closes #41.

This redesigns the RuleBasedNumberFormat and PluralFormat classes into static methods that replace the former implementation. The mapping old-to-new is as follows:

  1. ICU4N.Text.RuleBasedNumberFormat a. Rules Engine > ICU4N.Globalization.NumberFormatRules, an immutable class. The instances for SpellOut, Ordinal, Duration, and NumberingSystem are available as properties on the UNumberFormatInfo class which is returned from the UCultureInfo.NumberFormat property. b. Format method overloads > overloads of ICU4N.FormatNumberRuleBased.ToString() and ICU4N.FormatNumberRuleBased.TryFormat(), which are also also extension methods for all built-in .NET numeric data types.
  2. ICU4N.Text.DecimalFormatSymbols > ICU4N.Globalization.UNumberFormatInfo, which is available from the UCultureInfo.NumberFormat property.
  3. ICU4N.Text.NFRule > ICU4N.Globalization.NumberFormatRule.
  4. ICU4N.Text.NFRuleSet > ICU4N.Globalization.NumberFormatRuleSet.
  5. ICU4N.Text.NFSubstitution > ICU4N.Globalization.Substitution. All subclasses have also been copied to the ICU4N.Globalization namespace with their original names to subclass the new base class.

As a stand-in for DecimalFormat, we are using the custom formatting string feature in .NET. But since the .NET formatter doesn't support all of the settings that DecimalFormat has, these have been hidden from the public API until a complete implementation of DecimalFormat is constructed.

PluralFormat is also not available publicly as the MessagePattern class will need some refactoring to avoid unnecessary heap allocations.

This functionality is also currently only available on .NET 6+.

The old ICU4J APIs have been removed from the public API for NuGet releases. They can be enabled for custom builds by setting the MSBuild property IncludeLegacyNumberFormat=true in the build. Alternatively, this API is available in the https://www.nuget.org/packages/ICU4N/60.1.0-alpha.401 package. Do note there are known issues with rounding accuracy in this implementation of DecimalFormat.

The .NET formatter does not follow the IEEE 754 spec for rounding - it rounds toward positive infinity rather than to the nearest even number. So there may be issues with rounding in this new formatter for floating point types which will be addressed when we replace DecimalFormat.