Java Joy: Format Numbers In Compact Form
Since Java 12 we can format numbers in a compact style with the CompactNumberFormat
class in the java.text
package. A number like 23000 is formatted as 23K for the English locale. Instead of the short representation of K for 1000 we can also use a longer style where K is transformed as thousand for the English locale. We can use the same class to parse a String value that is in the compact style into a number.
In the following example we use several options of the CompactNumberFormat
class: