Thursday, July 17, 2014

Displaying amounts in differnt formats in JAVA, SQL and JSF

I'm working in a product company in Finance domain. We have a clients all across the globe.
Being a product company, it is required to cater the requirements of all clients as per their needs.

Following blog is very helpful to serve the amount display in various formats

One of the requirements is to show the amounts on screen in clients own format
e.g. INTEGER QTY: 123456    DOUBLE AMT: 345987.24 should be displayed as follows


LOCALE: English
INTEGER QTY: 123,456      DOUBLE AMT: 345,987.24

LOCALE: English (Canada)
INTEGER QTY: 123,456      DOUBLE AMT: 345,987.24

LOCALE: French (France)
INTEGER QTY: 123 456      DOUBLE AMT: 345 987,24

LOCALE: German (Germany)
INTEGER QTY: 123.456      DOUBLE AMT: 345.987,24

LOCALE: Italian (Italy)
INTEGER QTY: 123.456      DOUBLE AMT: 345.987,24

LOCALE: Japanese
INTEGER QTY: 123,456      DOUBLE AMT: 345,987.24

LOCALE: German (Germany)
INTEGER QTY: 123.456      DOUBLE AMT: 345.987,24

Prerequisite : We will have to set a locale per environment for the clients region e.g. FR, EN, DE, etc

JAVA :  In java we can format the field in specific locale format in String and use it to display in that locale's format as shown in below TestUtil.java class which is used to format numbers, amounts, rates, quantity, percentage, etc


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

public class TestUtil {

    static public void displayNumber(Locale currentLocale, Integer quantity,
            Double amount) {

        NumberFormat numberFormatter;
        String quantityOut;
        String amountOut;

        numberFormatter = NumberFormat.getNumberInstance(currentLocale);
        quantityOut = numberFormatter.format(quantity);
        amountOut = numberFormatter.format(amount);

        System.out.println("LOCALE: " + currentLocale.getDisplayName());
        System.out.println("INTEGER QTY: " + quantityOut);
        System.out.println("DOUBLE AMT: " + amountOut);
    }

    static public void displayCurrency(Locale currentLocale, Double currency) {

        NumberFormat currencyFormatter;
        String currencyOut;

        currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
        currencyOut = currencyFormatter.format(currency);
        System.out.println(currencyOut + "   " + currentLocale.toString());
    }

    static public void displayPercent(Locale currentLocale, Double percent) {

        NumberFormat percentFormatter;
        String percentOut;

        percentFormatter = NumberFormat.getPercentInstance(currentLocale);
        percentOut = percentFormatter.format(percent);
        System.out.println(percentOut + "   " + currentLocale.toString());
    }

    static public void main(String[] args) {

        Locale localeTmp = new Locale("de", "DE");
        Locale[] locales = { Locale.ENGLISH, Locale.CANADA, Locale.FRANCE,
                Locale.GERMANY, Locale.ITALY, Locale.JAPANESE, localeTmp };

        Integer quantity = new Integer(123456);
        Double amount = new Double(345987.24);
        Double currency = new Double(9876543.21);
        Double percent = new Double(0.75);

        System.out.println("INTEGER QTY: " + quantity);
        System.out.println("DOUBLE AMT: " + amount);
        // System.out.println("DOUBLE CURRENCY: " + currency);
        // System.out.println("DOUBLE PERCENTAGE: " + percent);

        for (int i = 0; i < locales.length; i++) {
            System.out.println();
            displayNumber(locales[i], quantity, amount);
            // displayCurrency(locales[i], currency);
            // displayPercent(locales[i], percent);
        }
    }

}



SQL : In SQL we have NLS (National Language Support) which can be used to fetch and alter current session’s values. Current values can be fetched by below query.

SELECT * FROM V$NLS_PARAMETERS;

--To alter language for current session:
ALTER SESSION SET NLS_LANGUAGE=GERMAN;
select * from nonexistent_table;

--To alter territory for current session
ALTER SESSION SET NLS_TERRITORY=GERMANY;

--This will display amount in this locale’s format.
select to_char(a.amount), a.amount from my_table a;


JSF : Simple change would be on all JSFs (jspx) files, wherever amounts are shown. We can use application level parameter for locale and show amounts in that locale’s format. For <f:convertNumber> tag, there is one attribute ‘locale’. Its value can be dynamically set from system params. Refer below code snippet with hardcoded parameter as 'DE'.



<h:outputText value="Total Amount" />
<h:panelGroup>
    <h:outputText value="#{mtBean.myVo.amount}">
        <f:convertNumber maxFractionDigits="2" minFractionDigits="2" locale="DE"/>
    </h:outputText>
</h:panelGroup>


No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...