Warning: Invalid argument supplied for foreach() in /home/techrecipes/public_html/wp-content/themes/techrecipes/header.php on line 77

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: [email protected]
-William. § (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

  • Iridium

    Thank you William, this was exceptionally helpful.

    • t4thilina

      how to use that DecimalFormat in a static method ?

  • Zhen Dong

    helpful and concise. :) . nice job.

  • DK

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

  • deta

    it’s helpful. thank you

  • Anonymous

    #java #programming number-formatting made simple

  • Nvehmann

    Made Java formula precise….thank you

  • PeterWadethespider

    Thanks guy. you made my highschool comp sci easier

  • Kainthalwayswin

    Thank you very much…it help me a lot

    • Piranha Urs

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

  • YSReddy

    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.

    • Dwight

      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.

      • answer

        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.

  • Boyu_y

    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.

  • will pham

    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

  • Matt

    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.

  • Cheri

    Hello William,

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

    Thank you.

  • desmond

    thank you all for your continuous help.God bless.

  • desmond

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

  • amer rahhal

    thans alot of for your informations