Java Currency Formatter

  • + 0 comments

    This solution passes validation for Java 8

    public class Solution {
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            double payment = scanner.nextDouble();
            scanner.close();
    
            // Create NumberFormat instances for each required locale
            NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
            NumberFormat indiaFormat = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
            NumberFormat chinaFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
            NumberFormat franceFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    
            // Format the payment value according to each locale
            String us = usFormat.format(payment);
            String india = indiaFormat.format(payment);
            String china = chinaFormat.format(payment);
            String france = franceFormat.format(payment);
    
            // Print each formatted currency value
            System.out.println("US: " + us);
            System.out.println("India: " + india);
            System.out.println("China: " + china);
            System.out.println("France: " + france);
            
    }
    }