Java Currency Formatter

Sort by

recency

|

865 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.text.*;

    public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double d = sc.nextDouble();

        Locale indiaLocale = new Locale("en", "IN");
    
        NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
        NumberFormat indiaFormat = NumberFormat.getCurrencyInstance(indiaLocale);
        NumberFormat chinaFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
        NumberFormat franceFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    
        System.out.println("US: " + usFormat.format(d));
        System.out.println("India: Rs." + indiaFormat.format(d).replace("₹", "").trim());
        System.out.println("China: " + chinaFormat.format(d));
        System.out.println("France: " + franceFormat.format(d));
    }
    

    }

    I've attempted this problem in five different ways, carefully formatting the output to match the expected results. Despite that, the solution consistently fails on several test cases.

    I finally submitted the best version I could, and it passed only 2 out of 8 test cases. At this point, I'm feeling completely stuck.

    If anyone has managed to pass all the test cases, I would really appreciate it if you could share your working code. It would help me understand what I'm missing. Thanks in advance!

  • + 0 comments
    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 india = new Locale("en", "IN");
    
            System.out.println("US: " + formatPayment(Locale.US, payment));
            System.out.println("India: " + formatPayment(india, payment));
            System.out.println("China: " + formatPayment(Locale.CHINA, payment));
            System.out.println("France: " + formatPayment(Locale.FRANCE, payment));
        }
    
        public static String formatPayment(Locale locale, double payment) {
            NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
            return nf.format(payment);
        }
    }
    
  • + 0 comments

    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();
    
    
        Locale india= new Locale("en", "IN");
    
    
        System.out.println("US: " + NumberFormat.getInstance(Locale.US).format(payment));
        System.out.println("India: " + NumberFormat.getInstance(india).format(payment));
        System.out.println("China: " + NumberFormat.getInstance(Locale.CHINA).format(payment));
        System.out.println("France: " + NumberFormat.getInstance(Locale.FRANCE).format(payment));
    }
    

    }

  • + 1 comment
    import java.util.*;
    import java.text.*;
    
    public class Solution {
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            double payment = scanner.nextDouble();
            scanner.close();
            
            Locale INDIA = new Locale("en", "IN");
    
            NumberFormat nfUS = NumberFormat.getCurrencyInstance(Locale.US);
            NumberFormat nfIndia = NumberFormat.getCurrencyInstance(INDIA);
            NumberFormat nfChina = NumberFormat.getCurrencyInstance(Locale.CHINA);
            NumberFormat nfFrance = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    
            String us = nfUS.format(payment);
            String india = nfIndia.format(payment);
            String china = nfChina.format(payment);
            String france = nfFrance.format(payment);
    
            System.out.println("US: " + us);
            System.out.println("India: " + india);
            System.out.println("China: " + china);
            System.out.println("France: " + france);
        }
    }
    
  • + 2 comments

    I really had a hard time solving this problem, really. In the end the simple solution was changing java 15 jdk for jdk8 or jdk7. (: