我编写了一个程序,以返回带有某些国家/地区的货币符号的双精度值。为此,我正在使用 name value Diff Count Index
0 A 1 0 2 1
1 A 3 2 2 2
2 B 1 0 4 1
3 B 2 1 4 2
4 B 3 2 4 3
5 B 1 0 4 4
6 C 2 0 3 1
7 C 3 1 3 2
8 C 3 1 3 3
方法来获取特定国家/地区的符号。
该问题特定于笔记本电脑的 JDK-1.8 ,并且在联机编译器上运行正常。 我面临的问题是 CHINA 和 FRANCE 的货币符号用'?'表示。但是对于 INDIA 和 US ,会显示正确的符号。
我现在正在解决这个问题。因此,任何线索都将有所帮助。
这是我的代码:
getCurrencyInstance()
我的机器上的相应输出是:
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
/* Create custom Locale for India.
Locale indiaLocale = new Locale("en", "IN");
/* Create NumberFormats using Locales */
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
/* Print output */
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));
}
}
答案 0 :(得分:1)
Hackerrank挑战中的问题是没有UTF-8编码。尝试使用此:
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
/* Create custom Locale for India.*/
Locale indiaLocale = new Locale("en", "IN");
/* Create NumberFormats using Locales */
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
/* Print output */
try {
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8"));
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));
} catch (Exception e) {
System.out.println("HackerRank please fix your test terminal");
}
}
}
答案 1 :(得分:0)
在中国使用此代码。
Numberformat类的getCurrency()。getSymbol(locale)将返回特定区域的货币符号。
System.out.println("China: " + china.getCurrency().getSymbol(Locale.CHINA) + china.format(payment));
在法国使用此
System.out.println("France: " + france.format(payment) + " " + france.getCurrency().getSymbol(Locale.FRANCE));
答案 2 :(得分:0)
解决方案将是这样; ......
对于印度卢比,没有货币。我们必须使用十进制格式设置其格式,如下所示。
DecimalFormat IndianCurrencyFormat = new DecimalFormat("##,##,###.00");
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat nf1 = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat nf2 = NumberFormat.getCurrencyInstance(Locale.FRANCE);
String us = nf.format(payment);
String india ="Rs." + IndianCurrencyFormat.format(payment);
String china =nf1.format(payment);
String france =nf2.format(payment);
System.out.println("US: " + us);
System.out.println("India: " + india);
System.out.println("China: " + china);
System.out.println("France: " + france);
答案 3 :(得分:0)
我的解决方案只是使子字符串摆脱了UTF-8问题
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
/*
* input 12324.134
* output
* US: $12,324.13
* India: Rs.12,324.13
* China: ¥12,324.13
* France: 12 324,13 €
*/
import java.util.Scanner;
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.
DecimalFormat formatter = new DecimalFormat("###,###.00");
double roundOff = Math.round(payment * 100) / 100.00;
String us = NumberFormat.getCurrencyInstance().format(roundOff);
String india = "Rs." + formatter.format(roundOff);
String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(roundOff);
String france_initial = NumberFormat.getCurrencyInstance(Locale.FRENCH).format(roundOff);
int len = france_initial.length();
String france = france_initial.substring(0, len - 1) + " €";
System.out.println("US: " + us);
System.out.println("India: " + india);
System.out.println("China: " + china);
System.out.println("France: " + france);
}
}
答案 4 :(得分:-1)
创建局部变量,因为印度没有货币代码 参考这个 - https://docs.oracle.com/javase/tutorial/i18n/locale/create.html
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();
Locale indiaLocale = new Locale("en", "IN");
// Write your code here.
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
if(payment>=0 || payment<=Math.pow(10,9)){
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));
}
}
}