Home  >  Article  >  Java  >  How to Format Currency in Indian Numbering System with Java?

How to Format Currency in Indian Numbering System with Java?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-25 08:44:02900browse

How to Format Currency in Indian Numbering System with Java?

Displaying Currency in Indian Numbering Format

In India, currency values are often represented in a unique numbering format where separators are placed after every two digits, except for the last set which is separated by thousands. To achieve this format in Java, the standard DecimalFormat class does not provide support for variable-width groups.

Unfortunately, traditional Java SE DecimalFormat falls short in this regard:

"If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####"."

To overcome this limitation, consider using ICU4J (International Components for Unicode for Java). Its NumberFormat class offers the desired feature:

<code class="java">Format format = com.ibm.icu.text.NumberFormat.getCurrencyInstance(new Locale("en", "in"));
System.out.println(format.format(new BigDecimal("100000000")));</code>

This code will output the value in the desired Indian numbering format:

Rs 10,00,00,000.00

Note that the com.ibm.icu.text.NumberFormat class extends the java.text.Format class, which provides the format(Object) method.

Alternatively, if you're working with Android, you can leverage the Android version of java.text.DecimalFormat, as it supports variable-width groups through its implementation using ICU internally.

The above is the detailed content of How to Format Currency in Indian Numbering System with Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn