Java Currency Formatter

  • [deleted]
    + 3 comments

    Am I misunderstanding how Locales work? I wasn't able to find a locale for india, and had to construct one using:

    NumberFormat inr = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
    

    for the US, I was able to just use

    NumberFormat usd = NumberFormat.getCurrencyInstance(Locale.US);
    

    My 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);
            double payment = scanner.nextDouble();
            scanner.close();
            
            NumberFormat usd = NumberFormat.getCurrencyInstance(Locale.US);
            System.out.println("US format = "+usd.format(payment));
            
            NumberFormat inr = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
            System.out.println("India format = "+inr.format(payment));
            
            
        }
    }
    
    • + 5 comments

      I couldn't solve it either. I used:

      NumberFormat.getCurrencyInstance(new Locale("in")).format(payment))
      

      and got a funny looking character instead of "Rs":

      ¤12.324,13
      
      • [deleted]
        + 1 comment
        NumberFormat inr = NumberFormat.getCurrencyInstance(new Locale("en", "in"));
        

        Works correctly and formats the currency as need, I just feel like India would have a built-in locale, so I'm wondering if I'm just overlooking it.

        • + 2 comments

          India does not have a built-in Locale; what you did was correct.

          • + 2 comments

            when India doesn't have Locale, how is it interpreting india as "RS." ? I am confused!!

            • + 0 comments

              I'm struggling with the same thing. Did you ever find an answer here?

            • + 0 comments

              She means a built-in locale like Locale.US - such a locale is so commonly used that it warrants being built-in. There are locales, however, for virtually every country, they just aren't all accessed in a 'built-in' way.

          • + 0 comments

            image what is wrong in the output can u explain?

      • + 0 comments

        Just change the LowerCase "in" to UpperCase. It'll Work.

      • + 0 comments

        The Locale class has 3 constructors, (String language), (String language, String country), (String language, String country, String variant)

        Since you only provided 1 string it called the (String language) constructor, which is why the symbol is incorrect

      • + 0 comments

        says cannot find the variable for India. Please help.

      • + 0 comments

        (new Locale("en","IN"))

        That should fix the symbol

    • + 0 comments

      I solved it in a similar way, expect for using printf() instead. I was suprised to find to out that there isn't any field for India baked into Java's Locale class unlike Locale.US and Locale.CHINA, etc. Oh, well...

      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);
              double payment = scanner.nextDouble();
              scanner.close();
              Locale US = new Locale("en", "US");
              Locale IN = new Locale("en", "IN");           
              System.out.printf("%s = %s\n%s = %s", 
                                "US format", 
                                NumberFormat.getCurrencyInstance(US).format(payment),
                                "India format", 
                                NumberFormat.getCurrencyInstance(IN).format(payment));
          }
      }