gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:14:30 28 4
gpt4 key购买 nike

我能够格式化美国和印度的货币。我已经尝试使用 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: $12,324.13
India: Rs.12,324.13
China: ?12,324.13
France: 12?324,13 ?

预期输出

    US: $12,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: $200.34
India: ₹ 200.34
China: ¥200.34
France: 200,34 €

更新:如果您仍然遇到此问题,可能是由于 Eclipse IDE 中的设置所致。检查是否 How to support UTF-8 encoding in Eclipse对你有帮助。

关于java - 为什么我无法格式化其他两个国家的货币中国和法国,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58886405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com