Hab*_*bib 2 java printing currency class currency-formatting
我目前正在执行一项任务,涉及获取代表一笔钱的双精度数字,并使用 NumberFormat 类的 getCurrencyInstance 方法将其转换为美国、印度、中国和法国的货币格式。
\n输入:
\nA single double-precision number denoting 'payment'\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nOn 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.\nRun Code Online (Sandbox Code Playgroud)\n输入示例:
\n12324.134\nRun Code Online (Sandbox Code Playgroud)\n示例输出:
\nUS: $12,324.13\n\nIndia: Rs.12,324.13\n\nChina: \xef\xbf\xa512,324.13\n\nFrance: 12 324,13 \xe2\x82\xac\nRun Code Online (Sandbox Code Playgroud)\n我的代码:
\nimport 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}\nRun Code Online (Sandbox Code Playgroud)\n我的输入:
\n12324.134\nRun Code Online (Sandbox Code Playgroud)\n我的输出:
\nUS: $12324.134\n\nIndia: Rs.12324.134\n\nChina: \xef\xbf\xa512324.134\n\nFrance: 12324.134 \xe2\x82\xac\nRun Code Online (Sandbox Code Playgroud)\n我的问题在于输入“12324.134”的格式。正如您所看到的,我已经成功输出了正确的货币符号,特别是印度,因为印度没有内置的语言环境,因此我必须构建一个语言为 en(即英语)的货币符号。
\n我似乎无法将逗号放在正确的位置,并且出于某种原因,法国的预期输出在前两位数字后有一个空格。这可能是因为我没有使用货币类的正确方法,但这就是我陷入困境的地方。任何建议表示赞赏。
\nNumberFormat.getCurrencyInstance 您可能应该使用NumberFormat.getCurrencyInstance和NumberFormat.format。尝试这个:
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}\nRun Code Online (Sandbox Code Playgroud)\n输出是:
\nUS: $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\nRun Code Online (Sandbox Code Playgroud)\n你的任务中甚至还提到:
\n\n\n使用 NumberFormat 类的 getCurrencyInstance 方法
\n