Home > Java > javaTutorial > body text

How to Format Currency in Indian Numbering System with Java?

Patricia Arquette
Release: 2024-10-25 08:44:02
Original
968 people have browsed it

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>
Copy after login

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

Rs 10,00,00,000.00
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template