为什么我无法格式化其他两个国家中国和法国的货币

why I am not able to format the currency of other two countries China and France

我可以格式化美国和印度的货币。我已经尝试了一些使用 Localebuilder 的实现,谁能解释一下我缺少什么。

 import java.io.*;
 import java.util.*;
 import java.text.*;
 import java.math.*;
 import java.util.regex.*;
 import java.text.NumberFormat;
 import java.util.Locale;

 public class Solution {

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

    // Write your code here.

    NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
    System.out.println("US: " + us.format(payment));

    Locale indiaa = new Locale("en", "IN");
    NumberFormat india = NumberFormat.getCurrencyInstance(indiaa);
    System.out.println("India: " + india.format(payment));

    Locale chinaLocale = new Locale.Builder().setLanguage("zh").setRegion("CN").build();
    NumberFormat china = NumberFormat.getCurrencyInstance(chinaLocale);
    System.out.println("China: " + china.format(payment));


    NumberFormat france =  NumberFormat.getCurrencyInstance(Locale.FRANCE);
    System.out.println("France: " + france.format(payment));
    }
    }

我得到的输出

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

预期输出

    US: ,324.13
    India: Rs.12,324.13
    China: ¥12,324.13
    France: 12 324,13 €

我还是初学者 提前致谢

Locale 中没有印度字段,因此,您需要为印度创建一个自定义字段 Locale。但是我没有看到你提到的 CHINAFRANCE 有任何这样的问题。请查看以下程序的输出:

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

public class Solution {
    public static void main(String[] args) {
        System.out.print("Enter an amount: ");
        Scanner scanner = new Scanner(System.in);
        double payment = scanner.nextDouble();
        scanner.close();

        Locale indiaLocale = new Locale("en", "IN");

        NumberFormat us     = NumberFormat.getCurrencyInstance(Locale.US);
        NumberFormat india  = NumberFormat.getCurrencyInstance(indiaLocale);
        NumberFormat china  = NumberFormat.getCurrencyInstance(Locale.CHINA);
        NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);

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

样本运行:

Enter an amount: 200.34
US: 0.34
India: ₹ 200.34
China: ¥200.34
France: 200,34 €

更新: 如果您仍然遇到这个问题,可能是因为您的 eclipse IDE 中的设置。检查 How to support UTF-8 encoding in Eclipse 是否对您有帮助。