Search code examples
javajava-8localenumber-formatting

NumberFormat.getCurrencyInstance() not returning currency symbol for Locales CHINA and FRANCE (jdk-1.8)


I have written a program to return double values with the currency symbols of some countries. For this I am using getCurrencyInstance() method to get symbol of particular country.

The problem is specific to my laptop's JDK-1.8 and works fine on online compiler. The problem, I am facing is that the currency symbol for CHINA and FRANCE are represented with '?'. But for INDIA and US, correct symbols are shown.

I am working on this problem for a bit now. Hence, any leads would be helpful.

Here is my code:

import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

public class Solution {

public static void main(String[] args) {
    /* Read input */
    Scanner scanner = new Scanner(System.in);
    double payment = scanner.nextDouble();
    scanner.close();

    /* Create custom Locale for India.
    Locale indiaLocale = new Locale("en", "IN");

    /* Create NumberFormats using Locales */
    NumberFormat us     = NumberFormat.getCurrencyInstance(Locale.US);
    NumberFormat india  = NumberFormat.getCurrencyInstance(indiaLocale);
    NumberFormat china  = NumberFormat.getCurrencyInstance(Locale.CHINA);
    NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);

    /* Print output */        
    System.out.println("US: "     + us.format(payment));
    System.out.println("India: "  + india.format(payment));
    System.out.println("China: "  + china.format(payment));
    System.out.println("France: " + france.format(payment));
}
}

The corresponding output on my machine is:

12324.134
US: $12,324.13
India: Rs.12,324.13
China: ?12,324.13
France: 12 324,13 ?

Solution

  • for china use this code. Numberformat class's getCurrency().getSymbol(locale) will return currency symbol for particular region. System.out.println("China: " + china.getCurrency().getSymbol(Locale.CHINA) + china.format(payment));

    For France use this System.out.println("France: " + france.format(payment) + " " + france.getCurrency().getSymbol(Locale.FRANCE));