Java Currency Formatter

Sort by

recency

|

836 Discussions

|

  • + 0 comments

    NumberFormat num1 = NumberFormat.getCurrencyInstance(Locale.US); String us = num1.format(payment); NumberFormat num2 = NumberFormat.getCurrencyInstance(new Locale("en","IN")); String india = num2.format(payment); NumberFormat num3 = NumberFormat.getCurrencyInstance(Locale.CHINA); String china = num3.format(payment); NumberFormat num4 = NumberFormat.getCurrencyInstance(Locale.FRANCE); String france = num4.format(payment);

  • + 0 comments

    Java 8 - Not optimal, but hopefully clear:

    import java.util.Scanner;
    import java.util.Locale;
    import java.text.NumberFormat;
    
    public class Solution {
        public static void main(String[] args) {
            // Scanner - For the "payment" input
            Scanner scanner = new Scanner(System.in);
            double payment = scanner.nextDouble();
            scanner.close();
            
            // Four NumberFormat instances - One for each locale
            NumberFormat f_US = NumberFormat.getCurrencyInstance(Locale.US);
            NumberFormat f_IN = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
            NumberFormat f_CH = NumberFormat.getCurrencyInstance(Locale.CHINA);
            NumberFormat f_FR = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    
            // Four System outputs - One for each locale
            System.out.println("US: " + f_US.format(payment));
            System.out.println("India: " + f_IN.format(payment));
            System.out.println("China: " + f_CH.format(payment));
            System.out.println("France: " + f_FR.format(payment));
        }
    }
    
  • + 0 comments

    i use Java 7 and it runs, i expect was helping

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

    public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double payment = scanner.nextDouble();
        scanner.close();
    
        NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
        System.out.println("US: " + us.format(payment));
    
        NumberFormat in = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
        System.out.println("India: " + in.format(payment));
    
        NumberFormat ch = NumberFormat.getCurrencyInstance(Locale.CHINA);
        System.out.println("China: " + ch.format(payment));
    
        NumberFormat fr = NumberFormat.getCurrencyInstance(Locale.FRANCE);
        System.out.println("France: " + fr.format(payment));
    }
    

    }

  • + 0 comments

    //Indian rupee Rs.1... was causing problem because my code was writing in hindi characters // at first. And second, third, etc. times as well //The next problem, when I thought I found the solution: //Error: I used a non-ASCII character ("₹") - Headshot, thanks Java8 //Finally there is a working solution (I got help from a smarter one, thx for him). I didn't know it "currInr.startsWith" //I hope I could help if someone would have need it

    import java.util.; import java.text.; import java.math.*;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        Scanner scanner = new Scanner(System.in);
        String amount = scanner.nextLine();
        scanner.close();
    
        Currency usd = Currency.getInstance("USD");
        Currency cny = Currency.getInstance("CNY");
        Currency eur = Currency.getInstance("EUR");
    
        Locale INDIA = new Locale("en", "IN");
        Currency inr = Currency.getInstance("INR");
    
        NumberFormat inrFormatter = NumberFormat.getCurrencyInstance(INDIA);
        inrFormatter.setCurrency(inr);
        inrFormatter.setMaximumFractionDigits(2);
        inrFormatter.setMinimumFractionDigits(2);
    
        NumberFormat usdFormatter = NumberFormat.getCurrencyInstance(Locale.US);
        usdFormatter.setCurrency(usd);
    
        NumberFormat eurFormatter = NumberFormat.getCurrencyInstance(Locale.FRANCE);
        eurFormatter.setCurrency(eur);
    
        NumberFormat cnyFormatter = NumberFormat.getCurrencyInstance(Locale.CHINA);
        cnyFormatter.setCurrency(cny);
    
        BigDecimal amt = new BigDecimal(amount);
        String currInr = inrFormatter.format(amt);
        if (!currInr.startsWith("Rs.")) {
            currInr = "Rs." + currInr.substring(1);
        }
        String currUs = usdFormatter.format(amt);
        String currFr = eurFormatter.format(amt);
        String currCn = cnyFormatter.format(amt);
    
        System.out.println("US: " + currUs);
        System.out.println("India: " + currInr);
        System.out.println("China: " + currCn);
        System.out.println("France: " + currFr);
    
    }
    

    }

  • + 0 comments

    If you are having problems using java 15 in this problem switch to java 7. It worked for me that way.