2020-02-19

Java NumberFormat Example

NumberFormat adds support for formatting a number in its compact form.

Compact number formatting refers to the representation of a number in a short or human readable form. For example, in the en_US locale, 1000 can be formatted as "1K" and 1000000 can be formatted as "1M", depending upon the style specified by NumberFormat.Style. The compact number formats are defined by LDML's specification for Compact Number Formats. To obtain an instance, use one of the factory methods given by NumberFormat for compact number formatting.

For example:

NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
String result = fmt.format(1000);

The example above results in "1K".

No comments:

Post a Comment