Java Currency Formatter

  • + 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!