Search code examples
javacurrencynumber-formatting

why I am not able to format the currency of other two countries China and France


I am able to format the currencies of US and India. I have tried the few implementations using the Localebuilder ,Can anyone please explain what ia m missing.

 import java.io.*;
 import java.util.*;
 import java.text.*;
 import java.math.*;
 import java.util.regex.*;
 import java.text.NumberFormat;
 import java.util.Locale;

 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.

    NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
    System.out.println("US: " + us.format(payment));

    Locale indiaa = new Locale("en", "IN");
    NumberFormat india = NumberFormat.getCurrencyInstance(indiaa);
    System.out.println("India: " + india.format(payment));

    Locale chinaLocale = new Locale.Builder().setLanguage("zh").setRegion("CN").build();
    NumberFormat china = NumberFormat.getCurrencyInstance(chinaLocale);
    System.out.println("China: " + china.format(payment));


    NumberFormat france =  NumberFormat.getCurrencyInstance(Locale.FRANCE);
    System.out.println("France: " + france.format(payment));
    }
    }

The Output I am getting

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

Expected Output

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

I am still a beginner Thanks in Advance


Solution

  • There is no field for INDIA in Locale and therefore, you need to create a custom Locale for INDIA. But I do not see any such problem with CHINA and FRANCE which you have mentioned. Please look at the output of the following program:

    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Scanner;
    
    public class Solution {
        public static void main(String[] args) {
            System.out.print("Enter an amount: ");
            Scanner scanner = new Scanner(System.in);
            double payment = scanner.nextDouble();
            scanner.close();
    
            Locale indiaLocale = new Locale("en", "IN");
    
            NumberFormat us     = NumberFormat.getCurrencyInstance(Locale.US);
            NumberFormat india  = NumberFormat.getCurrencyInstance(indiaLocale);
            NumberFormat china  = NumberFormat.getCurrencyInstance(Locale.CHINA);
            NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    
            System.out.println("US: "     + us.format(payment));
            System.out.println("India: "  + india.format(payment));
            System.out.println("China: "  + china.format(payment));
            System.out.println("France: " + france.format(payment));
        }
    }
    

    A sample run:

    Enter an amount: 200.34
    US: $200.34
    India: ₹ 200.34
    China: ¥200.34
    France: 200,34 €
    

    Update: If you are still facing the issue, it may be because of settings in your eclipse IDE. Check if How to support UTF-8 encoding in Eclipse helps you.