Java Currency Formatter - Hackerrank

Formatting Currency for Different Locales Using Java

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

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

Problem Statement

You are given a double-precision number, denoting an amount of money. You need to format this amount according to the currency formats of the US, India, China, and France, and then print the formatted values.

Input Format

A single double-precision number denoting the amount of money.

Output Format

On the first line, print US: formattedPayment where formattedPayment is formatted for US currency. On the second line, print India: formattedPayment where formattedPayment is formatted for Indian currency. On the third line, print China: formattedPayment where formattedPayment is formatted for Chinese currency. On the fourth line, print France: formattedPayment where formattedPayment is formatted for French currency.

Sample Input

12324.134

Sample Output

US: $12,324.13
India: Rs.12,324.13
China: ¥12,324.13
France: 12 324,13 €

Explanation

Each line contains the value of the amount formatted according to the respective currencies of the four countries.

Solution

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); // Initialize Scanner to read input
        double payment = scanner.nextDouble(); // Read the input amount
        scanner.close(); // Close the Scanner
        
        // Format the payment according to US currency format
        String us = NumberFormat.getCurrencyInstance(Locale.US).format(payment);
        
        // Format the payment according to Indian currency format
        String india = NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(payment);
        
        // Format the payment according to Chinese currency format
        String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment);
        
        // Format the payment according to French currency format
        String france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment);
        
        // Print the formatted currency values
        System.out.println("US: " + us);
        System.out.println("India: " + india);
        System.out.println("China: " + china);
        System.out.println("France: " + france);
    }
}

Explanation of Code:

  • Input Handling:

    • Scanner scanner = new Scanner(System.in); initializes the Scanner to read input.

    • double payment = scanner.nextDouble(); reads the input amount of money.

    • scanner.close(); closes the Scanner object after reading input to free resources.

  • Currency Formatting:

    • String us = NumberFormat.getCurrencyInstance(Locale.US).format(payment); formats the payment according to the US currency format.

    • String india = NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(payment); formats the payment according to the Indian currency format. Note that India does not have a built-in Locale, so we construct one with Locale("en","IN").

    • String china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment); formats the payment according to the Chinese currency format.

    • String france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment); formats the payment according to the French currency format.

  • Output:

    • The formatted currency values are printed for each locale using System.out.println.

This approach allows you to handle different currency formats using Java's NumberFormat class and the appropriate locales for each country.

Conclusion

By using the NumberFormat class in Java, we can easily convert and format currency values according to different locales. This is particularly useful for applications that deal with internationalization and need to display monetary values in various currency formats



**  Please do subscribe my blog for future updates and share with your friends, if you find this informative **

Comments

Popular Posts