Displaying Currency in Indian Numbering Format

2019-01-02 18:05发布

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. Any help in this matter will be greatly appreciated.

Thanks

15条回答
零度萤火
2楼-- · 2019-01-02 18:39

Unfortunately 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.

查看更多
十年一品温如言
3楼-- · 2019-01-02 18:40

If there is no default Locale available and the user doesn't make any change to the locale, we can go with setting the currency symbol using unicode and decimal formatting. As in the below code:

For e.g. Setting the Indian currency symbol and formatting the value. This will work without user making changes in the settings.

    Locale locale = new Locale("en","IN");
    DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
    dfs.setCurrencySymbol("\u20B9");
    decimalFormat.setDecimalFormatSymbols(dfs);
    System.out.println(decimalFormat.format(payment));

Output:

₹12,324.13
查看更多
梦醉为红颜
4楼-- · 2019-01-02 18:41

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.

查看更多
皆成旧梦
5楼-- · 2019-01-02 18:42
import java.util.*;

public class string1 {

     public static void main(String args[])
     {
         int i,j;
         boolean op=false;

         StringBuffer sbuffer = new StringBuffer();
         Scanner input = new Scanner(System.in);
         System.out.println("Enter a string");
         sbuffer.append(input.nextLine());

         int length=sbuffer.length();
         if(sbuffer.length()<3)
         {
             System.out.println("string="+sbuffer);
         }
         else
         {
             for ( i = sbuffer.length(); i >0; i--) 
             {

                 if (i==length-3)
                 {
                     sbuffer.insert(i, ",");
                      op=true;
                 }
                 while(i>1 && op==true)
                 {
                     i=i-2;

                     if(i>=1)
                     {

                     sbuffer.insert(i, ",");
                     }
                 }
             }

         }
         System.out.println("string="+sbuffer);
     }
}
查看更多
回忆,回不去的记忆
6楼-- · 2019-01-02 18:46
public String getIndianCurrencyFormat(String amount) {
    StringBuilder stringBuilder = new StringBuilder();
    char amountArray[] = amount.toCharArray();
    int a = 0, b = 0;
    for (int i = amountArray.length - 1; i >= 0; i--) {
        if (a < 3) {
            stringBuilder.append(amountArray[i]);
            a++;
        } else if (b < 2) {
            if (b == 0) {
                stringBuilder.append(",");
                stringBuilder.append(amountArray[i]);
                b++;
            } else {
                stringBuilder.append(amountArray[i]);
                b = 0;
            }
        }
    }
    return stringBuilder.reverse().toString();
}

This is what i did, for getting Indian currency format. if input is 1234567890 means output is 1,23,45,67,890.

查看更多
情到深处是孤独
7楼-- · 2019-01-02 18:47

Kotlin version, It works on Android API 26

fun currencyLocale(value: Double): String {
    val formatter = NumberFormat.getCurrencyInstance(Locale("en", "in"))
    return formatter.format(value)
}

fun parseCommaSeparatedCurrency(value: String): Number {
    return NumberFormat.getCurrencyInstance(Locale("en", "in")).parse(value)
}
查看更多
登录 后发表回答