Author: saqibkhan

  • DecimalFormat Class

    The java.text.DecimalFormat class is used for formatting numbers as per customized format and as per locale.

    Example – Format Numbers

    In this example, we’re formatting numbers based on a given pattern.

    Open Compiler

    importjava.text.DecimalFormat;publicclassI18NTester{publicstaticvoidmain(String[] args){String pattern ="####,####.##";double number =123456789.123;DecimalFormat numberFormat =newDecimalFormat(pattern);System.out.println(number);System.out.println(numberFormat.format(number));}}

    Output

    It will print the following result.

    1.23456789123E8
    1,2345,6789.12
    
  • Parse Numbers

    Example

    In this example, we’re showcasing parsing of number present in different locale.

    Open Compiler

    importjava.text.NumberFormat;importjava.text.ParseException;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args)throwsParseException{Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getInstance(daLocale);System.out.println(numberFormat.parse("100,76"));
    
    
      numberFormat =NumberFormat.getInstance(enLocale);System.out.println(numberFormat.parse("100,76"));}}</code></pre>

    Output

    It will print the following result.

    100.76
    10076
    
  • Set Rounding Mode

    Example

    In this example, we’re showcasing Rounding Mode.

    Open Compiler

    importjava.math.RoundingMode;importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");NumberFormat numberFormat =NumberFormat.getInstance(enLocale);
    
    
      numberFormat.setMinimumFractionDigits(0);
      numberFormat.setMaximumFractionDigits(0);System.out.println(numberFormat.format(99.50));
      numberFormat.setRoundingMode(RoundingMode.HALF_DOWN);System.out.println(numberFormat.format(99.50));}}</code></pre>

    Output

    It will print the following result.

    100
    99
    
  • Example

    In this example, we’re setting min and max digits for both integer as well as fractional part of a number.

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");NumberFormat numberFormat =NumberFormat.getInstance(enLocale);
    
      numberFormat.setMinimumIntegerDigits(2);
      numberFormat.setMaximumIntegerDigits(3);
      numberFormat.setMinimumFractionDigits(2);
      numberFormat.setMaximumFractionDigits(3);System.out.println(numberFormat.format(12234.763443));}}</code></pre>

    Output

    It will print the following result.

    234.763
    
  • Format Percentages

    Example

    In this example, we’re formatting numbers in percentage format.

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");NumberFormat numberFormat =NumberFormat.getPercentInstance(enLocale);System.out.println(numberFormat.format(0.76));}}

    Output

    It will print the following result.

    76%
    
  • Format Currencies

    In this example, we’re formatting currencies based on US locale and Danish Locale.

    Example

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getCurrencyInstance(daLocale);System.out.println(numberFormat.format(100.76));
    
    
      numberFormat =NumberFormat.getCurrencyInstance(enLocale);System.out.println(numberFormat.format(100.76));}}</code></pre>

    Output

    It will print the following result.

    kr 100,76
    $100.76
    
  • NumberFormat Class

    The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country. For example, In Denmark fractions of a number are separated from the integer part using a comma whereas in England they use a dot as separator.

    Example – Format Numbers

    In this example, we’re formatting numbers based on US locale and Danish Locale.

    Open Compiler

    importjava.text.NumberFormat;importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale enLocale =newLocale("en","US");Locale daLocale =newLocale("da","DK");NumberFormat numberFormat =NumberFormat.getInstance(daLocale);System.out.println(numberFormat.format(100.76));
    
    
      numberFormat =NumberFormat.getInstance(enLocale);System.out.println(numberFormat.format(100.76));}}</code></pre>

    Output

    It will print the following result.

    100,76
    100.76
    
  • ResourceBundle Class

    ResourceBundle class is used to store text and objects which are locale sensitive. Generally we use property files to store locale specific text and then represent them using ResourceBundle object. Following are the steps to use locale specific properties file in a java based application.

    Step 1: Create Properties Files

    Suppose we need properties file for English locale. Then create a properties file name XXX_en_US.properties where XXX is the name of the file and en_US represents the locale for English(US).

    Messages_en_US.properties

    message=WelcometoTutorialsPoint.COM!

    Let’s now create properties file for French locale. Then create a properties file name XXX_fr_FR.properties where XXX is the name of the file and fr_FR represents the locale for French(France).

    Messages_fr_FR.properties

    message=Bienvenue sur TutorialsPoint.COM!

    Here you can figure out that the key is same but the value is locale specific in both the properties file.

    Step 2: Create ResourceBundle Object

    Create ResourceBundle object with properties file name and locale using following syntax.

    ResourceBundle bundle =ResourceBundle.getBundle("Messages",Locale.US);

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Step 3: Get the value from ResourceBundle Object

    Get the value from ResourceBundle object by passing the key.

    String value = bundle.getString("message");

    Example

    Following example illustrate the use of ResourceBundle objects to display locale specific values from properties files.

    Open Compiler

    importjava.util.Locale;importjava.util.ResourceBundle;publicclassI18NTester{publicstaticvoidmain(String[] args){ResourceBundle bundle =ResourceBundle.getBundle("Messages",Locale.US);System.out.println("Message in "+Locale.US+": "+bundle.getString("message"));  
    
    
      bundle =ResourceBundle.getBundle("Messages",Locale.FRANCE);System.out.println("Message in "+Locale.FRANCE+": "+bundle.getString("message"));}}</code></pre>

    Output

    It will print the following result.

    Message in en_US: Welcome to TutorialsPoint.COM!
    Message in fr_FR: Bienvenue sur TutorialsPoint.COM!
    

    Notes for Naming Conventions

    Following are the naming conventions for the properties file.

    • For properties file mapped to default locale, no prefix is mandatory. message_en_US.properties is equivalent to message.properties.
    • For properties file mapped to locale, prefix can be attached in two ways. message_fr.properties is equivalent to message_fr_FR.properties.
  • Locale Language

    Example

    In this example, we’ll get display language per locale passed as an argument.

    Open Compiler

    importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale defaultLocale =Locale.getDefault();Locale enLocale =newLocale("en","US");Locale frLocale =newLocale("fr","FR");Locale esLocale =newLocale("es","ES");System.out.println(defaultLocale.getDisplayLanguage(enLocale));System.out.println(defaultLocale.getDisplayLanguage(frLocale));System.out.println(defaultLocale.getDisplayLanguage(esLocale));}}

    Output

    It will print the following result.

    English
    anglais
    inglés
    
  • Locale Details

    Example

    In this example, we’ll get default locale and print its details. Then create a locale for “fr” and print its details.

    Open Compiler

    importjava.util.Locale;publicclassI18NTester{publicstaticvoidmain(String[] args){Locale locale =Locale.getDefault();System.out.println("Default Locale Properties:\n");System.out.println(locale.getDisplayCountry());System.out.println(locale.getDisplayLanguage());System.out.println(locale.getDisplayName());System.out.println(locale.getISO3Country());System.out.println(locale.getISO3Language());System.out.println(locale.getLanguage());System.out.println(locale.getCountry());Locale frenchLocale =newLocale("fr","fr");System.out.println("\nfr Locale Properties:\n");System.out.println(frenchLocale.getDisplayCountry());System.out.println(frenchLocale.getDisplayLanguage());System.out.println(frenchLocale.getDisplayName());System.out.println(frenchLocale.getISO3Country());System.out.println(frenchLocale.getISO3Language());System.out.println(frenchLocale.getLanguage());System.out.println(frenchLocale.getCountry());}}

    Output

    It will print the following result.

    Default Locale Properties:
    
    United States
    English
    English (United States)
    USA
    eng
    en
    US
    
    fr Locale Properties:
    
    France
    French
    French (France)
    FRA
    fra
    fr
    FR