Home  >  Article  >  Java  >  How to Format Indian Rupee Currency with Variable-Width Groups in Java?

How to Format Indian Rupee Currency with Variable-Width Groups in Java?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 08:13:28282browse

How to Format Indian Rupee Currency with Variable-Width Groups in Java?

Formatting Indian Rupee Currency in Variable-Width Groups

In India, currency values are typically formatted differently than in other countries. For instance, a value like 450500 would be displayed as 4,50,500, with separators after every two digits except for the last set, which is in thousands.

Java's standard DecimalFormat class, however, does not support variable-width groups for formatting numbers. Solutions using locale or specific patterns do not fully address this issue.

Solution using ICU4J

To achieve this formatting in Java, the International Components for Unicode (ICU4J) library provides a NumberFormat class that supports variable-width groups.

<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 produces the desired output:

Rs 10,00,00,000.00

Note for Android Development

The Android version of DecimalFormat uses ICU under the hood and supports the feature described above.

The above is the detailed content of How to Format Indian Rupee Currency with Variable-Width Groups in 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