DAK
DAK

Reputation: 372

How to get formatted currency value for India?

For example in USA 1000000 represented as 1,000,000 (1 million) but in India it is represented as 10,00,000 (10 lakh). For this I tried some methods

double value = 1000000d;
NumberFormat numberFormatter = NumberFormat.getNumberInstance(new Locale("en", "IN"));
System.out.println(numberFormatter.format(value));
            

NumberFormat deci = new DecimalFormat("#,##,##,###.##");
System.out.println("Decimal Format "+deci.format(value));

NumberFormat format = NumberFormat.getNumberInstance();
format.setCurrency(Currency.getInstance("INR"));
format.setMaximumFractionDigits(2);      
System.out.println(format.format(value));

But all these methods give an output as 1,000,000 but I require format 10,00,000. What do I need to change here?

Upvotes: 0

Views: 534

Answers (2)

Naresh P
Naresh P

Reputation: 1

public static String fmt(String s)
{
    String formatted = "";
    if(s.length() > 1){
        formatted = s.substring(0,1);
        s = s.substring(1);
    }
    
    while(s.length() > 3){
        formatted += "," + s.substring(0,2);
        s = s.substring(2);
    }
    return formatted + "," + s + ".00"; 
}

Upvotes: 0

Gatusko
Gatusko

Reputation: 2598

Java decimal formatter doesn't support groups From Docs :

The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. 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 "#,##,###,####" == "######,####" == "##,####,####".

You will need another library for this. Suggest this : http://site.icu-project.org/ https://mvnrepository.com/artifact/com.ibm.icu/icu4j/69.1

Code

double value = 1000000d;
NumberFormat numberFormatter = NumberFormat.getNumberInstance(new Locale("en", "IN"));
System.out.println(numberFormatter.format(value));

Output :

10,00,000

Upvotes: 2

Related Questions