Friday, December 20, 2013

Currency formatting

Sometimes we need to work with money in our applications. As usual showed simple format using site or user locale. But what to do if we need to format the money amount with multiply currencies for one locale?

I've created simple project to show the formatting logic. You can download it from Git_Repo.

Need to say, not all currencies have a coins, so we have to handle this point.

java.util.Locale contain the information about currensy (delimiters, coins, etc)

DecimalFormat, NumberFormat - used for amount formatting

private String getResult(double amount, Locale siteLocale, Locale quoteLocale) {

    DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(siteLocale);
    Currency currency = Currency.getInstance(quoteLocale);
    
    //Get the string currency symbol from locale   
    String symbol = currency.getSymbol(quoteLocale);

    DecimalFormatSymbols smb = new DecimalFormatSymbols(quoteLocale);
    smb.setCurrencySymbol(symbol);

    //Set the currency symbol to site locale
    decimalFormat.setDecimalFormatSymbols(smb);
    decimalFormat.getMaximumFractionDigits();
    decimalFormat.getMinimumFractionDigits();

    return decimalFormat.format(amount);
}

And a result:


Thursday, November 21, 2013

Git for newcomers

Git allows a team of people to work together, all using the same files. And it helps the team cope with the confusion that tends to happen when multiple people are editing the same files.

Resources to learn it shown below.

First you need to install GIT CLIENT

Online trainings:

Books:
I think it's quite enough to start use git.