Displaying Currency in Indian Numbering Format
here is simple thing u can do ,
float amount = 100000;
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
String moneyString = formatter.format(amount);
System.out.println(moneyString);
The output will be Rs.100,000.00.
I also got myself in same problem.
I was working with DecimalFormat
.
I have no knowledge of JSTL
but you can figure out something by my solution.
As, grouping size remains constant in DecimalFormat
. I separated both parts, formatted them with different patterns and concat both. Here is the code.
public static String format(double value) {
if(value < 1000) {
return format("###", value);
} else {
double hundreds = value % 1000;
int other = (int) (value / 1000);
return format(",##", other) + ',' + format("000", hundreds);
}
}
private static String format(String pattern, Object value) {
return new DecimalFormat(pattern).format(value);
}
It will provide format like Indian Numbering System.
If you want decimal points, just add ".##"
in both conditions.
"###"
to "###.##"
and "000"
to "000.##"
.
Unfortunately on standard Java SE DecimalFormat
doesn't support variable-width groups. So it won't ever format the values exactly as you want to:
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
"#,##,###,####" == "######,####" == "##,####,####"
.
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 NumberFormat
class that does support this formatting:
Format format = com.ibm.icu.text.NumberFormat.getCurrencyInstance(new Locale("en", "in"));
System.out.println(format.format(new BigDecimal("100000000")));
This code will produce this output:
Rs 10,00,00,000.00
Note: the com.ibm.icu.text.NumberFormat
class does not extend the java.text.NumberFormat
class (because it already extends an ICU-internal base class), it does however extend the java.text.Format
class, which has the format(Object)
method.
Note that the Android version of java.text.DecimalFormat
class 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).
With Android, this worked for me:
new DecimalFormat("##,##,##0").format(amount);
450500 gets formatted as 4,50,500
http://developer.android.com/reference/java/text/DecimalFormat.html - DecimalFormat supports two grouping sizes - the primary grouping size, and one used for all others.