Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Thursday, May 5, 2011

Different Locales (US, Europe) in Talend Open Studio

The Java Runtime Environment is fully localized, and that capability can be used in Talend Open Studio jobs.

Java uses the Locale class to handle locale-specific data formats for currency, date, numbers, and messages.  Locale is a combination of country and language that identifies a particular combination of formats for internationalization  This blog post shows an internationalized handling of number formats.

In the U.S., decimal numbers use a dot for a decimal separator and a comma for a thousands separator.

Ex. 1,000.25007401

Other countries represent decimal numbers using the comma as a decimal separator and a dot for a thousands separator.

Ex. 1.000,25007401

There are other variations.  A space could be used for a thousands separator.  There may be no thousands separator.

To handle this in Talend Open Studio, a tMap can invoke Java parsing routines that are driven by a Locale.  In this tMap example, two variables are declared 'usVar' and 'itVar' that are String constants of different number formats.  The tMap will use the NumberFormat class to parse the numbers into a double.

Locale Used in a Talend tMap
From the input, a parse() method is called on a NumberFormat object.  The NumberFormat is based on a Locale; many Locale constants are available: FRENCH, ITALY, SIMPLIFIED_CHINESE, UK.  Read the Javadoc for the Locale class for a full listing.  Sometimes, a country (FRANCE) and a language (FRENCH) seem to be redundant.  Other times, this is more relevant (US/UK/ENGLISH).

The output schema is using a Double.  If using a BigDecimal for additional precision or integration with other libraries, see the Talend Forum page http://www.talendforge.org/forum/viewtopic.php?id=15502.

If these classes -- NumberFormat, Locale -- are used repeatedly, it's worthwhile to import the classes using a tJava.  On the "Advanced settings" tab, put "import java.util.Locale" and "import java.text.NumberFormat" (no quotes).  Then, the package of java.text.NumberFormat can be removed.

You can handle your internationalized data using Java's Locale class.  This example converted String numbers in different formats to a common Java type (Double).  Other uses of Locale include Date (month/day/year versus day/month/year), currency ($100 versus €100), or messages.

11 comments:

  1. Hello Carl,

    Once again, excellent post on Talend Open Studio.

    Thanks a lot,
    Talend Community Manager.

    ReplyDelete
  2. your post enabled me to solve my actual number formatting problem in talend, thanks carl from alois

    ReplyDelete
  3. Glad to have helped alois. -Carl

    ReplyDelete
  4. wanted to receive an email, Hi Carl, how would you place the thousands comma in the double?

    ReplyDelete
  5. Your article is too good and informative. Keep writing and sharing educational article like this which can help us to grow our knowledge.

    JAVA Training In Pune

    ReplyDelete
  6. Thanks for sharing with us. I just loved your way of presentation. I enjoyed reading this .Thanks for sharing and keep writing. It is good to read blogs like this.
    Rubix Research

    ReplyDelete
  7. Wow what an article please keep sharing such content.
    Java Classes in Pune

    ReplyDelete
  8. One of the best things i came across on the internet. Thanks for sharing.
    Web Development Classes in Pune
    Android Classes in Pune
    Web Design Classes in Pune

    ReplyDelete
  9. Hi for big Decimal what will be the formula ? thanks

    ReplyDelete