0

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

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
K.Sindhu
  • 57
  • 1
  • 10
  • 2
    Please [edit] your question to include the problem and question you have. – Progman Nov 16 '19 at 00:36
  • I added expected vs what I am getting and I clearly mentioned all the code I am using and I mentioned in the question itself about the two countries. – K.Sindhu Nov 16 '19 at 00:54
  • 2
    Could be a font problem; maybe your font has no euro sign. –  Nov 16 '19 at 00:56
  • Can you say where you are running your code? Are you using the command line? Eclipse? IntelliJ? – Freiheit Nov 16 '19 at 01:01
  • @another-dave I am running it on hacker rank. My code is not showing any errors. Do you think not having euro sign would be the problem – K.Sindhu Nov 16 '19 at 01:01
  • @Freiheit If I run it on eclipse even Indian currency is not showing up – K.Sindhu Nov 16 '19 at 01:05
  • @Freiheit 123 US: $123.00 India: ? 123.00 China: ?123.00 France: 123,00 € – K.Sindhu Nov 16 '19 at 01:05
  • @ArvindKumarAvinash Yeah, I tried your code too, I think some font problem. Because when I use ur code, I am not getting the currency for India and China – K.Sindhu Nov 16 '19 at 01:17
  • @ArvindKumarAvinash yeah, I got it after changing the settings in eclipse. Thank You – K.Sindhu Nov 16 '19 at 01:25

1 Answers1

2

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • I did create Locale for India. But when i am running the code my output is as I mentioned above – K.Sindhu Nov 16 '19 at 00:57
  • 1
    I tried Arvinds code at https://www.jdoodle.com/online-java-compiler/ and it works correctly. – Freiheit Nov 16 '19 at 01:01
  • 1
    @Freiheit I think it is the font problem because when i try Arvinds code on eclipse still I am not getting the proper output – K.Sindhu Nov 16 '19 at 01:11
  • @Freiheit Enter an amount: 123 US: $123.00 India: ? 123.00 China: ?123.00 France: 123,00 € – K.Sindhu Nov 16 '19 at 01:11