Java: Decimal Format to Create Custom Output Easily

The Decimal Format class is a poorly explained class in the Java Documentation, so I will clear up a little of it with an example.


You first need to import the DecimalFormat class:

import java.text.DecimalFormat;

Then create a format object. This object can be used with doubles, as it uses a decimal. It uses a template String to teach Java how to ouput the objects.
*This example will be used as a standard format of money:

DecimalFormat money = new DecimalFormat("$0.00");

Now that you have a money object (declared either globally or locally) you can now format your objects.
*Note: This example uses only two decimal places and a $, but any symbols or length is available.

As an example of printing a double value:

double amount = 4.333333333;
System.out.println(money.format(amount));

What will be printed is $4.33 as described by the template. This handles your precision and dollar sign with a single template.

Questions/Comments: william_a_wilson@hotmail.com
-William. § (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

20 Responses to “Java: Decimal Format to Create Custom Output Easily”

  1. January 29, 2009 at 1:29 am, Iridium said:

    Thank you William, this was exceptionally helpful.

    Reply

    • November 20, 2010 at 4:52 pm, t4thilina said:

      how to use that DecimalFormat in a static method ?

      Reply

  2. March 06, 2009 at 1:22 pm, Zhen Dong said:

    helpful and concise. :) . nice job.

    Reply

  3. May 02, 2009 at 1:40 am, DK said:

    wow..that was so simple…and easy to undastand…
    thanks..

    Reply

  4. July 13, 2009 at 7:25 am, deta said:

    it’s helpful. thank you

    Reply

  5. September 02, 2010 at 6:37 pm, Anonymous said:

    #java #programming number-formatting made simple

    Reply

  6. September 17, 2010 at 4:13 am, Nvehmann said:

    Made Java formula precise….thank you

    Reply

  7. September 23, 2010 at 7:50 pm, PeterWadethespider said:

    Thanks guy. you made my highschool comp sci easier

    Reply

  8. September 29, 2010 at 5:50 pm, Kainthalwayswin said:

    Thank you very much…it help me a lot

    Reply

    • January 19, 2011 at 3:51 pm, Piranha Urs said:

      Hi, this example works in Windows environment, but when we deploy in the unix environment, we doesn’t get $ symbol … can anyone explain me …

      Reply

  9. January 28, 2011 at 7:27 am, YSReddy said:

    Hi

    DecimalFormat formatter = new DecimalFormat(“#,##,##,##0.00″);
    double num = 1212245.67;
    System.out.println(formatter.format(num));

    The above code is showing as 1,212,245.67, but it should be 12,12,245.67

    Anybody please help me with correct code.

    Reply

    • July 14, 2012 at 6:12 pm, Dwight said:

      Re the question about using ‘DecimalFormat(“#,##,##,##0.00″);’ — the specification says:
      ‘The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So “#,##,###,####” == “######,####” == “##,####,####”. ‘
      So you cannot get DecimalFormat to generate output with different number of digits in each group.

      Reply

      • January 20, 2013 at 10:25 am, answer said:

        Yes you can.

        developer.android.com/reference/java/text/DecimalFormat.html

        The grouping separator is a character that separates clusters of integer digits to make large numbers more legible. It is commonly used for thousands, but in some locales it separates ten-thousands. The grouping size is the number of digits between the grouping separators, such as 3 for “100,000,000″ or 4 for “1 0000 0000″. There are actually two different grouping sizes: One used for the least significant integer digits, the primary grouping size, and one used for all others, the secondary grouping size. In most locales these are the same, but sometimes they are different. For example, if the primary grouping interval is 3, and the secondary is 2, then this corresponds to the pattern “#,##,##0″, and the number 123456789 is formatted as “12,34,56,789″. If a pattern contains multiple grouping separators, the interval between the last one and the end of the integer defines the primary grouping size, and the interval between the last two defines the secondary grouping size. All others are ignored, so “#,##,###,####”, “###,###,####” and “##,#,###,####” produce the same result.

        Reply

  10. May 27, 2011 at 3:16 pm, Boyu_y said:

    The first comma to decimal point is 3 digits, so 1,212,245.67 is the correct result, if you change the pattern to #,##,##,##.00, the output should be 1,21,22,45.67. The number is always divided in equal size.

    Reply

  11. October 20, 2011 at 10:45 am, will pham said:

    how do i print 1, 2, 3, ……, 8, 9. as 01, 02, 03, ……, 08, 09
    i have an array of size 10. myArray[10]
    so far i have
    for(int i = 0; i

    Reply

  12. November 18, 2011 at 10:09 pm, Matt said:

    Awesome! Thanks for making this so clear. The other examples I found online confused the issue with a lot of unnecessary code. Simple is better for a Java noob like me.

    Reply

  13. March 03, 2012 at 6:36 pm, Cheri said:

    Hello William,

    This was extremely helpful. After searching for an hour I found you site and was able to complete my program.

    Thank you.

    Reply

  14. March 27, 2012 at 4:57 am, desmond said:

    thank you all for your continuous help.God bless.

    Reply

  15. March 27, 2012 at 4:58 am, desmond said:

    thank you all for the good work you offer to us all.God you more

    Reply

  16. May 03, 2012 at 8:54 am, amer rahhal said:

    thans alot of for your informations

    Reply

Leave a Reply