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".

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation