We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class CurrencyFormatter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read input
double payment = sc.nextDouble(); // For example, we use a fixed number. In actual use, this could be read from input.
// US Locale
Locale us = Locale.US;
NumberFormat usFormat = NumberFormat.getCurrencyInstance(us);
String usFormatted = usFormat.format(payment);
// India Locale
Locale india = new Locale("en", "IN");
NumberFormat indiaFormat = NumberFormat.getCurrencyInstance(india);
String indiaFormatted = indiaFormat.format(payment);
// China Locale
Locale china = Locale.CHINA;
NumberFormat chinaFormat = NumberFormat.getCurrencyInstance(china);
String chinaFormatted = chinaFormat.format(payment);
// France Locale
Locale france = Locale.FRANCE;
NumberFormat franceFormat = NumberFormat.getCurrencyInstance(france);
String franceFormatted = franceFormat.format(payment);
// Print results
System.out.println("US: " + usFormatted);
System.out.println("India: " + indiaFormatted);
System.out.println("China: " + chinaFormatted);
System.out.println("France: " + franceFormatted);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Currency Formatter
You are viewing a single comment's thread. Return to all comments →
This Answer Works in JAVA 8 :
import java.text.NumberFormat; import java.util.Locale; import java.util.Scanner;
public class CurrencyFormatter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read input double payment = sc.nextDouble(); // For example, we use a fixed number. In actual use, this could be read from input.
}