Java Currency Formatter

Sort by

recency

|

822 Discussions

|

  • + 0 comments

    This Answer Works in JAVA 8 :

    import java.text.NumberFormat; import java.util.Locale; import java.util.Scanner;

    public class CurrencyFormatter { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read input double payment = sc.nextDouble(); // For example, we use a fixed number. In actual use, this could be read from input.

        // US Locale
        Locale us = Locale.US;
        NumberFormat usFormat = NumberFormat.getCurrencyInstance(us);
        String usFormatted = usFormat.format(payment);
    
        // India Locale
        Locale india = new Locale("en", "IN");
        NumberFormat indiaFormat = NumberFormat.getCurrencyInstance(india);
        String indiaFormatted = indiaFormat.format(payment);
    
        // China Locale
        Locale china = Locale.CHINA;
        NumberFormat chinaFormat = NumberFormat.getCurrencyInstance(china);
        String chinaFormatted = chinaFormat.format(payment);
    
        // France Locale
        Locale france = Locale.FRANCE;
        NumberFormat franceFormat = NumberFormat.getCurrencyInstance(france);
        String franceFormatted = franceFormat.format(payment);
    
        // Print results
        System.out.println("US: " + usFormatted);
        System.out.println("India: " + indiaFormatted);
        System.out.println("China: " + chinaFormatted);
        System.out.println("France: " + franceFormatted);
    }
    

    }

  • + 0 comments

    I wasted too much time in this problem. Still couldn't find the solution. May be better to focus on other problems.

  • + 0 comments

    This is so broken, don't bother doing in anything other then java 8. i think this question need changing for each verstion. this was every furstrating as i wasted 20 miuntes undering why my correct answer was wrong, even after changing the China: ¥ to the China: ¥ and the India: Rs.

  • + 0 comments

    I got my submission to pass by switching to Java 8 from java 15. But I left my workarounds for some of the problems. HackerRank shoul have different answers for the different Java versions, like they do with different SQL varieties.

    Three differences: * Java 15 uses the Rupee symbol, but the answer wanted "Rm.". Had to create a custom currency fix for that. * Java 15 uses a different "Yen"/"Yuan" symbol than Java 8. * Java 15 uses a "NARROW NO-BREAK SPACE" (U+202F) code point for the grouping/thousands chaSPracter. The answer used a regular space character SPACE (U+0020).

  • + 0 comments

    This solution passes validation for Java 8

    public class Solution {
        
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            double payment = scanner.nextDouble();
            scanner.close();
    
            // Create NumberFormat instances for each required locale
            NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);
            NumberFormat indiaFormat = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
            NumberFormat chinaFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
            NumberFormat franceFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    
            // Format the payment value according to each locale
            String us = usFormat.format(payment);
            String india = indiaFormat.format(payment);
            String china = chinaFormat.format(payment);
            String france = franceFormat.format(payment);
    
            // Print each formatted currency value
            System.out.println("US: " + us);
            System.out.println("India: " + india);
            System.out.println("China: " + china);
            System.out.println("France: " + france);
            
    }
    }