I have a question about formatting the Rupee currency (Indian Rupee – INR).
Typically a value like 450500 is formatted and shown as 450,500. In India, the same value is displayed as 4,50,500
For example, numbers here are represented as:
1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000
Refer Indian Numbering System
The separators are after two digits, except for the last set, which is in thousands.
I’ve searched on the internet and people have asked to use the locale en_GB or pattern #,##,##,##,##0.00
I tried this on JSTL by using the following tag:
<fmt:formatNumber value="${product.price}" type="currency"
pattern="#,##,##,##,###.00"/>
But this does not seem to solve the issue.
Unfortunately on standard Java SE
DecimalFormatdoesn’t support variable-width groups. So it won’t ever format the values exactly as you want to:Most number formatting mechanisms in Java are based on that class and therefore inherit this flaw.
ICU4J (the Java version of the International Components for Unicode) provides a
NumberFormatclass that does support this formatting:This code will produce this output:
Note: the
com.ibm.icu.text.NumberFormatclass does not extend thejava.text.NumberFormatclass (because it already extends an ICU-internal base class), it does however extend thejava.text.Formatclass, which has theformat(Object)method.Note that the Android version of
java.text.DecimalFormatclass is implemented using ICU under the hood and does support the feature in the same way that the ICU class itself does (even though the summary incorrectly mentions that it’s not supported).