使用“NumberFormat”类 Java 进行国家/地区货币格式化

问题描述 投票:0回答:1

我目前正在执行一项任务,涉及获取代表一笔钱的双精度数字,并使用 NumberFormat 类的 getCurrencyInstance 方法将其转换为美国、印度、中国和法国的货币格式。

输入:

A single double-precision number denoting 'payment'

输出:

On the first line, print US: 'u' where 'u' is 'payment' formatted for US currency.
On the second line, print India: 'i' where 'i' is 'payment' formatted for the Indian currency.
On the third line, print China: 'c' where 'c' is 'payment' formatted for Chinese currency.
On the fourth line, print France: 'f' where 'f' is formatted for French currency.

输入示例:

12324.134

输出示例:

US: $12,324.13

India: Rs.12,324.13

China: ¥12,324.13

France: 12 324,13 €

我的代码:

import java.util.*;
import java.text.*;
import java.util.Currency;

public class Solution {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double payment = scanner.nextDouble();
        scanner.close();
        
        Locale indiaLocale = new Locale("en", "IN");
        
        Locale locale = Locale.US;
        Currency us = Currency.getInstance(locale);
        String symbol = us.getSymbol(locale);
        System.out.println("US: " + symbol + payment);
        
        Locale locale2 = indiaLocale;
        Currency in = Currency.getInstance(locale2);
        String symbol2 = in.getSymbol(locale2);
        System.out.println("India: " + symbol2 + payment);
        
        Locale locale3 = Locale.CHINA;
        Currency ch = Currency.getInstance(locale3);
        String symbol3 = ch.getSymbol(locale3);
        System.out.println("China: " + symbol3 + payment);
        
        Locale locale4 = Locale.FRANCE;
        Currency fr = Currency.getInstance(locale4);
        String symbol4 = fr.getSymbol(locale4);
        System.out.println("France: " + payment + " " + symbol4);
    }
}

我的输入:

12324.134

我的输出:

US: $12324.134

India: Rs.12324.134

China: ¥12324.134

France: 12324.134 €

我的问题在于输入的格式“12324.134”。正如您所看到的,我已经成功输出了正确的货币符号,特别是印度,因为印度没有内置的语言环境,因此我必须构建一个语言为 en(即英语)的货币符号。

我似乎无法将逗号放在正确的位置,并且出于某种原因,法国的预期产出在前两位数字后有一个空格。这可能是因为我没有使用货币类的正确方法,但这就是我陷入困境的地方。任何建议表示赞赏。

java class printing currency currency-formatting
1个回答
0
投票

您可能应该使用

NumberFormat.format
。试试这个:

public static void main(String[] args) {
    double payment = 12324.134;

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

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

    Locale locale2 = indiaLocale;
    Currency in = Currency.getInstance(locale2);
    String symbol2 = in.getSymbol(locale2);
    System.out.println("India: " + NumberFormat.getCurrencyInstance(locale2).format(payment));

    Locale locale3 = Locale.CHINA;
    Currency ch = Currency.getInstance(locale3);
    String symbol3 = ch.getSymbol(locale3);
    System.out.println("China: " + NumberFormat.getCurrencyInstance(locale3).format(payment));

    Locale locale4 = Locale.FRANCE;
    Currency fr = Currency.getInstance(locale4);
    String symbol4 = fr.getSymbol(locale4);
    System.out.println("France: " + NumberFormat.getCurrencyInstance(locale4).format(payment));
}

输出为:

US: $12,324.13
India: ₹12,324.13
China: ¥12,324.13
France: 12 324,13 €
© www.soinside.com 2019 - 2024. All rights reserved.