06 Feb Java Currency Formatter
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}
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). A single double-precision number denoting payment. 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.
12324.134 {codeBox}
US: $12,324.13 India: Rs.12,324.13 China: ¥12,324.13 France: 12 324,13 € {codeBox}
Each line contains the value of payment formatted according to the four countries’ respective currencies.
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);
}
}
Approach II:
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);
}
}
Approach III:
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));
}
}
Java Tag Content Extractor HackerRank Solution Java BigDecimal HackerRank Solution Java String Introduction HackerRank Solution Java String Tokens – HackerRank Solution Java Pattern Syntax Checker HackerRank Solution 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.
Sorry, the comment form is closed at this time.