Class RoundedMetricPrefixFormat

java.lang.Object
java.text.Format
host.anzo.commons.text.formatters.RoundedMetricPrefixFormat
All Implemented Interfaces:
Serializable, Cloneable

public class RoundedMetricPrefixFormat extends Format
Converts a number to a string in metric prefix format. For example, 7800000 will be formatted as '7.8M'. Numbers under 1000 will be unchanged. Refer to the tests for further examples.
See Also:
  • Constructor Details

    • RoundedMetricPrefixFormat

      public RoundedMetricPrefixFormat()
  • Method Details

    • format

      public StringBuffer format(@NotNull @NotNull Object obj, @NotNull @NotNull StringBuffer output, @NotNull @NotNull FieldPosition pos)
      Specified by:
      format in class Format
    • parseObject

      public Object parseObject(String source, @NotNull @NotNull ParsePosition pos)
      Convert a String produced by format() back to a number. This will generally not restore the original number because format() is a lossy operation, e.g.
       
       def formatter = new RoundedMetricPrefixFormat()
       Long number = 5821L
       String formattedNumber = formatter.format(number)
       assert formattedNumber == '5.8k'
      
       Long parsedNumber = formatter.parseObject(formattedNumber)
       assert parsedNumber == 5800
       assert parsedNumber != number
       
       
      Specified by:
      parseObject in class Format
      Parameters:
      source - a number that may have a metric prefix
      pos - if parsing succeeds, this should be updated to the index after the last parsed character
      Returns:
      a Number if the the string is a number without a metric prefix, or a Long if it has a metric prefix