Friday, 8 July 2022

How to get the decimal grouping character for given locale?

Below snippet return the decimal grouping separator for given locale.

private static Character getDecimalGroupingChar(Locale locale) {
	DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(locale);
	DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
	Character groupingChar = symbols.getGroupingSeparator();
	return groupingChar;
}

 


Find the below working application.

 

DecimalGroupingCharDemo.java
package com.sample.app;

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

public class DecimalGroupingCharDemo {
	private static final double NUMBER = 987654321.123;

	private static Character getDecimalGroupingChar(Locale locale) {
		DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(locale);
		DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
		Character groupingChar = symbols.getGroupingSeparator();

		System.out.println(locale + "\t\t\t'" + groupingChar + "'\t" + format.format(NUMBER));

		return groupingChar;
	}

	public static void main(String[] args) {
		System.out.println("Country  decimalGroupingCharacter  " + NUMBER);

		getDecimalGroupingChar(Locale.GERMAN);
		getDecimalGroupingChar(new Locale("fi"));
		getDecimalGroupingChar(new Locale("en_IN"));

	}

}

 

Output

Country  decimalGroupingCharacter  9.87654321123E8
de			'.'	987.654.321,123
fi			' '	987 654 321,123
en_in			','	987,654,321.123

 

 

 

You may like

Miscellaneous

Extract private and public key from keystore

Public, private key generation in Java

Export public key certificate from keystore

Read pkcs12 certificate information in Java

How to Get Client Certificate from HttpServletRequest

Programmatically import certificate to cacerts file in Java

Sign a jar file using jarsigner tool

No comments:

Post a Comment