ChoiceFormat: Numeric range formatting

Customize the formatting of numeric ranges with ChoiceFormat patterns for arrays, strings, and more

1 2 Page 2
Page 2 of 2
  • When using the ChoiceFormat(double[], String[]) constructor, the two passed-in arrays must be of equal size or else an IllegalArgumentException ("Array and limit arrays must be of the same length.") will be thrown.
  • The "limits" double[] array provided to the ChoiceFormat(double[], String[]) constructorconstructor should have the limits listed from left-to-right in ascending numerical order. When this is not the case, no exception is thrown, but the logic is almost certainly not going to be correct as Strings being formatted against the instance of ChoiceFormat will "match" incorrectly. This same expectation applies to the constructor accepting a pattern.
  • ChoiceFormat allows Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY to be used for specifying lower range limits via its two-arrays constructor.
  • ChoiceFormat allows \u221E and -\u221E to be used for specifying lower range limits via its single String (pattern) constructor.
  • The ChoiceFormat constructor accepting a String pattern is a bit more flexible than the two-arrays constructor and allows one to specify lower limit boundaries as everything over a certain amount without including that certain amount exactly.
  • Symbols and characters with special meaning in the String patterns provided to the single StringChoiceFormat constructor include #<\u2264 (), \u221E (), and |.

Conclusion

ChoiceFormat allows formatting of numeric ranges to be customized so that specific ranges can have different and specific representations. This post has covered several different aspects of numeric range formatting with ChoiceFormat, but parsing numeric ranges from Strings using ChoiceFormat was not covered in this post.

Further reading

This story, "ChoiceFormat: Numeric range formatting" was originally published by marxsoftware.blogspot.com.

1 2 Page 2
Page 2 of 2