Java Currency Formatter

  • + 0 comments

    This Answer Works in JAVA 8 :

    import java.text.NumberFormat; import java.util.Locale; import java.util.Scanner;

    public class CurrencyFormatter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read input double payment = sc.nextDouble(); // For example, we use a fixed number. In actual use, this could be read from input.

        // US Locale
        Locale us = Locale.US;
        NumberFormat usFormat = NumberFormat.getCurrencyInstance(us);
        String usFormatted = usFormat.format(payment);
    
        // India Locale
        Locale india = new Locale("en", "IN");
        NumberFormat indiaFormat = NumberFormat.getCurrencyInstance(india);
        String indiaFormatted = indiaFormat.format(payment);
    
        // China Locale
        Locale china = Locale.CHINA;
        NumberFormat chinaFormat = NumberFormat.getCurrencyInstance(china);
        String chinaFormatted = chinaFormat.format(payment);
    
        // France Locale
        Locale france = Locale.FRANCE;
        NumberFormat franceFormat = NumberFormat.getCurrencyInstance(france);
        String franceFormatted = franceFormat.format(payment);
    
        // Print results
        System.out.println("US: " + usFormatted);
        System.out.println("India: " + indiaFormatted);
        System.out.println("China: " + chinaFormatted);
        System.out.println("France: " + franceFormatted);
    }
    

    }