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

Hab*_*bib 2 java printing currency class currency-formatting

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

\n

输入:

\n
A single double-precision number denoting 'payment'\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
On the first line, print US: 'u' where 'u' is 'payment' formatted for US currency.\nOn the second line, print India: 'i' where 'i' is 'payment' formatted for the Indian currency.\nOn the third line, print China: 'c' where 'c' is 'payment' formatted for Chinese currency.\nOn the fourth line, print France: 'f' where 'f' is formatted for French currency.\n
Run Code Online (Sandbox Code Playgroud)\n

输入示例:

\n
12324.134\n
Run Code Online (Sandbox Code Playgroud)\n

示例输出:

\n
US: $12,324.13\n\nIndia: Rs.12,324.13\n\nChina: \xef\xbf\xa512,324.13\n\nFrance: 12 324,13 \xe2\x82\xac\n
Run Code Online (Sandbox Code Playgroud)\n

我的代码:

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

我的输入:

\n
12324.134\n
Run Code Online (Sandbox Code Playgroud)\n

我的输出:

\n
US: $12324.134\n\nIndia: Rs.12324.134\n\nChina: \xef\xbf\xa512324.134\n\nFrance: 12324.134 \xe2\x82\xac\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

\n

And*_*min 5

NumberFormat.getCurrencyInstance

\n

您可能应该使用NumberFormat.getCurrencyInstanceNumberFormat.format。尝试这个:

\n
public static void main(String[] args) {\n    double payment = 12324.134; // I\'d use BigDecimal instead of double here, BTW. See the comment from another user under your post.\n\n    Locale us = Locale.US;\n    System.out.println("US: " + NumberFormat.getCurrencyInstance(us).format(payment));\n\n    Locale india = new Locale("en", "IN");\n    System.out.println("India: " + NumberFormat.getCurrencyInstance(india).format(payment));\n\n    Locale china = Locale.CHINA;\n    System.out.println("China: " + NumberFormat.getCurrencyInstance(china).format(payment));\n\n    Locale france = Locale.FRANCE;\n    System.out.println("France: " + NumberFormat.getCurrencyInstance(france).format(payment));\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出是:

\n
US: $12,324.13\nIndia: \xe2\x82\xb912,324.13\nChina: \xc2\xa512,324.13\nFrance: 12\xe2\x80\xaf324,13\xc2\xa0\xe2\x82\xac\n
Run Code Online (Sandbox Code Playgroud)\n

你的任务中甚至还提到:

\n
\n

使用 NumberFormat 类的 getCurrencyInstance 方法

\n
\n

  • 您应该使用 NumberFormat.getCurrencyInstance 是正确的。还应该注意的是,这里根本不需要Currency 类,因此可以从代码中删除所有Currency.getInstance 行和所有getSymbol 行。 (3认同)