Java Currency Formatter - Hacker Rank Solution
Hello Friends, How are you? Today I am going to solve the HackerRank Java Currency Formatter Problem with a very easy explanation. This is the 13th problem of Java on HackerRank. In this article, you will get more than one approach to solve this problem. So let's start-
{tocify} $title={Table of Contents}
HackerRank Java Currency Formatter - Problem Statement
Given a double-precision number, payment, denoting an amount of money, use the NumberFormat class' getCurrencyInstance method to convert the 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 {codeBox}
where formattedPayment is payment formatted according to the appropriate Locale's currency.
Note: India does not have a built-in Locale, so you must construct one where the language is en (i.e., English).
Input Format
A single double-precision number denoting payment.
Constraints
- 0 <= payment <= 10^9
Output Format
On the first line, print US: u where u is payment formatted for US currency.
On the second line, print India: i where i is payment formatted for the Indian currency.
On the third line, print China: c where c is payment formatted for Chinese currency.
On the fourth line, print France: f, where f is payment formatted for French currency.
Sample Input
12324.134 {codeBox}
Sample Output
US: $12,324.13 India: Rs.12,324.13 China: ¥12,324.13 France: 12 324,13 € {codeBox}
Explanation
Each line contains the value of payment formatted according to the four countries' respective currencies.
Java Currency Formatter - Hacker Rank Solution
Approach I:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
// NumberFormat format = new NumberFormat();
NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
String us = NumberFormat.getCurrencyInstance(Locale.US).format(payment);
String india = NumberFormat.getCurrencyInstance(new Locale("en", "IN")).format(payment);
String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment);
String france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment);
// Write your code here.
System.out.println("US: " + us);
System.out.println("India: " + india);
System.out.println("China: " + china);
System.out.println("France: " + france);
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
String us = NumberFormat.getCurrencyInstance(Locale.US).format(payment);
String india = NumberFormat.getCurrencyInstance(new Locale("en","in")).format(payment);
String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment);
String france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment);
// Write your code here.
System.out.println("US: " + us);
System.out.println("India: " + india);
System.out.println("China: " + china);
System.out.println("France: " + france);
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
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.
System.out.println("US: " + NumberFormat.getCurrencyInstance(new Locale("en","US")).format(payment));
System.out.println("India: " + NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(payment));
System.out.println("China: " + NumberFormat.getCurrencyInstance(new Locale("zh","CN")).format(payment));
System.out.println("France: " + NumberFormat.getCurrencyInstance(new Locale("fr","FR")).format(payment));
}
}
Also Read:
Disclaimer: The above Problem (Java Currency Formatter) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.
I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Java Currency Formatter HackerRank Problem and try to solve it again.
All the Best!
Tags:
HackerRank Java