Thursday, October 27, 2011

Locale Number Formats in Java

Java has an in-built class in the java.text package that allows you to output numbers according to your computer's locale. 
import java.text.NumberFormat;
import java.util.Locale;
The NumberFormat class allows you to easily output real numbers, integers, percents and currency according to your default computer locale or a specified locale.

Real numbers example:
        double nr = 19.333;
        NumberFormat localeFormat 
                        = NumberFormat.getInstance();
        NumberFormat germanFormat 
                        = NumberFormat.getNumberInstance(Locale.GERMAN);
        NumberFormat usFormat 
                        = NumberFormat.getNumberInstance(Locale.US);
        System.out.println( "Number formats: ");
        System.out.println( "German format: " + germanFormat.format(nr) 
                           + "\n" 
                           +"US format:    " + usFormat.format(nr)
                           + "\n"
                           +"Locale format " + localeFormat.format(nr));

        /*==Output==
            Number formats: 
            German format: 19,333
            US format:    19.333
            Locale format 19.333
        */ 
As you can see in the output comment, the german format uses the ',' character instead of the '.' used in the US for displaying the floating point. 

Currency example:
        double nr = 19.333; 

        NumberFormat localeCurrencyFormat 
                        = NumberFormat.getCurrencyInstance();
        NumberFormat ukCurrencyFormat  
                        = NumberFormat.getCurrencyInstance(Locale.UK);
        NumberFormat japanCurrencyFormat
                        = NumberFormat.getCurrencyInstance(Locale.JAPAN);

        System.out.println(  "Currency formats: ");
        System.out.println(  "UK currency format: " 
                              + ukCurrencyFormat.format(nr) 
                              + "\n" 
                              + "Japan currency format: " 
                              + japanCurrencyFormat.format(nr)
                              + "\n"
                              +"Locale currency format " 
                              + localeCurrencyFormat.format(nr));
        /*==Output==
         Currency formats: 
         UK currency format: £19.33
         Japan currency format: ¥19
         Locale currency format $19.33
         */
For the percent and integer number format, there isn't much difference between locals (as far as I know and tried), but these methods may prove in some cases useful:

Percent and integer example:
        double nr2=392.326;
        int nr1 = 44;
        NumberFormat localePercentFormat 
                    = NumberFormat.getPercentInstance();
        NumberFormat localeIntegerFormat
                    = NumberFormat.getIntegerInstance();
        System.out.println("Percent and integer format : "  
                           + localePercentFormat.format(nr1) + " "
                           + localePercentFormat.format(nr2)
                           + "\n"
                           +"Integer format : " 
                           + localeIntegerFormat.format(nr1) + " "
                           + localeIntegerFormat.format(nr2));
        /*==Output== 
         Percent format : 4,400% 39,233%
         Integer format : 44 392
        */
A NumberFormat object also allows you to set the minimum number of digits after and before the floating point by using the methods:
  • setMaximumFractionDigits(int digits);
    • Sets the maximum number of digits after the floating point. If your number has more digits after the floating point than specified, he will be truncated.
  • setMinimumFractionDigits(int digits);
    • Sets the minimum number of digits before the floating point. If your number has less digits after the floating than specified, extra zeroes will be added to the left.
  • setMaximumIntegerDigits(int digits);
    • Sets the maximum number of digits before the floating point. If your number has more digits after the floating point than specified, he will be truncated.
  • setMinimumIntegerDigits(int digits);
    • Sets the minimum number of digits before the floating point. If your number has less digits after the floating than specified, extra zeroes will be added to the right
Minimum and maximum digits example:
         double nr1 = 44.2;
        double nr2 = 4392.326;
        NumberFormat localeNumberFormat 
                    = NumberFormat.getNumberInstance();
        localeNumberFormat.setMaximumFractionDigits(4);
        localeNumberFormat.setMinimumFractionDigits(2);
        localeNumberFormat.setMaximumIntegerDigits(3);
        localeNumberFormat.setMinimumIntegerDigits(3);
        System.out.println("Number formats: ");
        System.out.println("Percent format : "  
                           + localeNumberFormat.format(nr1) + " "
                           + localeNumberFormat.format(nr2)
                           + "\n"
                           +"Integer format : " 
                           + localeNumberFormat.format(nr1) + " "
                           + localeNumberFormat.format(nr2));
        /*==Output==    
            Number formats: 
            Percent format : 044.20 392.326
            Integer format : 044.20 392.326
        */

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger