Home > Java > javaTutorial > body text

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

Mary-Kate Olsen
Release: 2024-10-25 08:13:28
Original
295 people have browsed it

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

This code produces the desired output:

Rs 10,00,00,000.00
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!