Saturday 30 January 2021

Java12: CompactNumberFormat: Convert decimal number to compact form

CompactNumberFormat class is introduced in Java12 and is used to format a decimal number in its compact form. This compact form of a number is suitable to display large numbers where the display space is limited.

 

Is compact form of a number locale specific?

Yes, compact form of a number is locale specific. For example following table represents the compact form of a number 1000 in different locales.

 

Locale

Compact form

India

हज़ार

America

1K

German

1.000

 

How to get the compact form of a number?

Step 1: Get instance of CompactNumberFormat class.

NumberFormat indiaCompactFormat = NumberFormat.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT);

 

Step 2: Call format method with a decimal number as argument.

String indiaCompactFormatResult = indiaCompactFormat.format(1000);

 

Find the below working application.

 

CompactFormExample.java

package com.sample.app.numbers;

import java.text.NumberFormat;
import java.util.Locale;

public class CompactFormExample {

	public static void main(String args[]) {
		NumberFormat indiaCompactFormat = NumberFormat.getCompactNumberInstance(new Locale("hi", "IN"),
				NumberFormat.Style.SHORT);
		NumberFormat usCompactFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
		NumberFormat germanCompactFormat = NumberFormat.getCompactNumberInstance(Locale.GERMAN,
				NumberFormat.Style.SHORT);

		String indiaCompactFormatResult = indiaCompactFormat.format(1000);
		String usCompactFormatResult = usCompactFormat.format(1000);
		String germanCompactFormatResult = germanCompactFormat.format(1000);

		System.out.println("locale\tcompactform");
		System.out.println("INDIA\t" + indiaCompactFormatResult);
		System.out.println("US\t" + usCompactFormatResult);
		System.out.println("GERMANY\t" + germanCompactFormatResult);
	}
}

 

Output

locale	compactform
INDIA	1 हज़ार
US	1K
GERMANY	1.000

 

Compact number styles

There are two styles supported.

a.   SHORT

b.   LONG

For example, a SHORT style compact number instance in the US locale formats 1000000  as "1M". However, a LONG style instance in same locale formats 10000 as "1 million".

 

Find the below working application.

 

CompactFormStylesExample.java

 

package com.sample.app.numbers;

import java.text.NumberFormat;
import java.util.Locale;

public class CompactFormStylesExample {
	public static void main(String args[]) {
		long thousand = 1000l;
		long tenThousand = 10000l;
		long oneLaksh = 100000l;
		long tenLaksh = 1000000l;
		long oneCrore = 10000000l;
		long tenCrore = 100000000l;
		long hundredCrore = 1000000000l;
		long thousandCrore = 10000000000l;

		NumberFormat usShortStyle = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
		NumberFormat usLongStyle = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);

		System.out.println("number\t\tshort_form\tlong_form");
		System.out.println(thousand + "\t\t" + usShortStyle.format(thousand) + "\t\t" + usLongStyle.format(thousand));
		System.out.println(
				tenThousand + "\t\t" + usShortStyle.format(tenThousand) + "\t\t" + usLongStyle.format(tenThousand));
		System.out.println(oneLaksh + "\t\t" + usShortStyle.format(oneLaksh) + "\t\t" + usLongStyle.format(oneLaksh));
		System.out.println(tenLaksh + "\t\t" + usShortStyle.format(tenLaksh) + "\t\t" + usLongStyle.format(tenLaksh));
		System.out.println(oneCrore + "\t" + usShortStyle.format(oneCrore) + "\t\t" + usLongStyle.format(oneCrore));
		System.out.println(tenCrore + "\t" + usShortStyle.format(tenCrore) + "\t\t" + usLongStyle.format(tenCrore));
		System.out.println(hundredCrore + "\t" + usShortStyle.format(hundredCrore) + "\t\t" + usLongStyle.format(hundredCrore));
		System.out.println(thousandCrore + "\t" + usShortStyle.format(thousandCrore) + "\t\t" + usLongStyle.format(thousandCrore));

		
	}

}


Output

number		short_form	long_form
1000		1K		1 thousand
10000		10K		10 thousand
100000		100K		100 thousand
1000000		1M		1 million
10000000	10M		10 million
100000000	100M		100 million
1000000000	1B		1 billion
10000000000	10B		10 billion

 

Reference

https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/text/CompactNumberFormat.html

http://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats

 

 


 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment