Class RoundedMetricPrefixFormat
java.lang.Object
java.text.Format
host.anzo.commons.text.formatters.RoundedMetricPrefixFormat
- All Implemented Interfaces:
Serializable,Cloneable
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.Format
Format.Field -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionformat(@NotNull Object obj, @NotNull StringBuffer output, @NotNull FieldPosition pos) parseObject(String source, @NotNull ParsePosition pos) Convert a String produced by format() back to a number.Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
Constructor Details
-
RoundedMetricPrefixFormat
public RoundedMetricPrefixFormat()
-
-
Method Details
-
format
public StringBuffer format(@NotNull @NotNull Object obj, @NotNull @NotNull StringBuffer output, @NotNull @NotNull FieldPosition pos) -
parseObject
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:
parseObjectin classFormat- Parameters:
source- a number that may have a metric prefixpos- 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
-