Skip to content

Instantly share code, notes, and snippets.

@jimmyFlash
Created January 7, 2020 10:51
Show Gist options
  • Save jimmyFlash/8c48a573bc1c093780cd528d5a6f68dd to your computer and use it in GitHub Desktop.
Save jimmyFlash/8c48a573bc1c093780cd528d5a6f68dd to your computer and use it in GitHub Desktop.
Formatting currency in java, standard and non-standard using country ISO code
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
import java.util.Scanner;
public class CurrencyFormatter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
String us = formateWithCurrencySymb(Locale.US, payment, "");
String india = formateWithCurrencySymb(Locale.ENGLISH, payment, "IN");
String china = formateWithCurrencySymb(Locale.CHINA, payment, "");
String france = formateWithCurrencySymb(Locale.FRANCE, payment, "");
System.out.println("US: " + us);
System.out.println("India: " + india);
System.out.println("China: " + china);
System.out.println("France: " + france);
}
private static String formateWithCurrencySymb(Locale locale, double payment, String country){
Locale loco = locale;
if(country.trim().length() > 0){
//System.out.println(locale.getLanguage());
loco = new Locale(locale.getLanguage(), country );
}
NumberFormat nf = NumberFormat.getCurrencyInstance(loco);
Currency currency = nf.getCurrency();
Currency currencySymbole = Currency.getInstance(currency.toString());
return nf.format(payment);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment