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

谁说我不能喝 提交于 2019-11-30 09:50:47

问题


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 ?

回答1:


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));




回答2:


The problem in Hackerrank challenge is that there is no UTF-8 encoding. Try to use this:

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 */
    try {
      System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));        
      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));
    } catch (Exception e) {
      System.out.println("HackerRank please fix your test terminal");
    }
  }
}


来源:https://stackoverflow.com/questions/54573057/numberformat-getcurrencyinstance-not-returning-currency-symbol-for-locales-chi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!