HackerRank Java- Currency Formatter



Given a double-precision number, payment, denoting an amount of money, use the NumberFormat class' getCurrencyInstance method to convert payment into the US, Indian, Chinese, and French currency formats. Then print the formatted values as follows:

US: formattedPayment
India: formattedPayment
China: formattedPayment
France: formattedPayment



  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. import java.util.Scanner;
  7. import java.text.NumberFormat;
  8. import java.util.Locale;
  9. public class Solution {
  10. public static void main(String[] args) {
  11. /* Save input */
  12. Scanner scan = new Scanner(System.in);
  13. double payment = scan.nextDouble();
  14. scan.close();
  15. /* Create custom Locale for India - I used the "IANA Language Subtag Registry" to find India's country code */
  16. Locale indiaLocale = new Locale("en", "IN");
  17. /* Create NumberFormats using Locales */
  18. NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
  19. NumberFormat india = NumberFormat.getCurrencyInstance(indiaLocale);
  20. NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
  21. NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
  22. /* Print output */
  23. System.out.println("US: " + us.format(payment));
  24. System.out.println("India: " + india.format(payment));
  25. System.out.println("China: " + china.format(payment));
  26. System.out.println("France: " + france.format(payment));
  27. }
  28. }

download android app