Formatting a Number Using a Custom Format

A pattern of special characters is used to specify the format of the number. This example demonstrates some of the characters. For a complete listing, see the javadoc documentation for the DecimalFormat class.

There is no symbol that either displays a digit or a blank if no digit present. Hence, it is not possible to format a number so that it will have a specific width. To achieve a specific width, you must manually pad the formatted number.

// The 0 symbol shows a digit or 0 if no digit present
NumberFormat formatter = new DecimalFormat("000000");
String s = formatter.format(-1234.567);  // -001235
// notice that the number was rounded up

// The # symbol shows a digit or nothing if no digit present
formatter = new DecimalFormat("##");
s = formatter.format(-1234.567);         // -1235
s = formatter.format(0);                 // 0
formatter = new DecimalFormat("##00");
s = formatter.format(0);                 // 00


// The . symbol indicates the decimal point
formatter = new DecimalFormat(".00");
s = formatter.format(-.567);             // -.57
formatter = new DecimalFormat("0.00");
s = formatter.format(-.567);             // -0.57
formatter = new DecimalFormat("#.#");
s = formatter.format(-1234.567);         // -1234.6
formatter = new DecimalFormat("#.######");
s = formatter.format(-1234.567);         // -1234.567
formatter = new DecimalFormat(".######");
s = formatter.format(-1234.567);         // -1234.567
formatter = new DecimalFormat("#.000000");
s = formatter.format(-1234.567);         // -1234.567000


// The , symbol is used to group numbers
formatter = new DecimalFormat("#,###,###");
s = formatter.format(-1234.567);         // -1,235
s = formatter.format(-1234567.890);      // -1,234,568

// The ; symbol is used to specify an alternate pattern for negative values
formatter = new DecimalFormat("#;(#)");
s = formatter.format(-1234.567);         // (1235)

// The ' symbol is used to quote literal symbols
formatter = new DecimalFormat("'#'#");
s = formatter.format(-1234.567);         // -#1235
formatter = new DecimalFormat("'abc'#");
s = formatter.format(-1234.567);         // -abc1235

Comments

18 Mar 2010 - 12:14am by foo (not verified)

You can format a number to a specific length like this:
System.out.format("%5s", Integer.toString(n));

14 Aug 2010 - 10:19am by Anonymous (not verified)

@foo only if you want to print to the stdout the message.

28 Dec 2010 - 8:09am by Anonymous (not verified)

else you can use String.format("%5s", Integer.toString(n));

26 Aug 2011 - 1:45pm by Prudence (not verified)

Great hammer of Thor, that is poewfrully helpful!

6 Sep 2011 - 6:38am by Anonymoose (not verified)

Error on the first line of code, "NumberFormat" should be "DecimalFormat".

13 Oct 2011 - 6:30am by ET (not verified)

If I want to format the following
123456789,547
to look like this
123 456 789,55
the I should use
DecimalFormat("#,##0.00")
correct? Assuming, in currency, I always want to show 2 decimal places and a zero if the overall value is zero?
i.e
1 would be formatted to 1.00
0 would be 0.00

If the above is correct, then - it doesn't work for me :(

8 Dec 2011 - 1:53am by Anyone (not verified)

@ET:
if you want to format like that, you have to change the grouping spereator to spaces, so it would be:
String format = "#,###,##0.00";
DecimalFormatSymbols forSpace = new DecimalFormatSymbols();
forSpace.setGroupingSeparator(' ');
DecimalFormat formatter = new DecimalFormat(format, forSpace);

14 Dec 2011 - 5:47am by Anonymous (not verified)

hello

14 Dec 2011 - 5:48am by Anonymous (not verified)

some people

27 Dec 2011 - 5:46am by jai (not verified)

if i want to format 123.567 as 123.57 and 700 as 700.00
what should i do?

26 Jan 2012 - 3:39pm by Anonymous (not verified)

I would like to create a format which allows engineering notation when required. For example, 5 would remain as 5 rather than 5E0 and 1234 would be 1.234E3. Is there a way to prevent the E0 from appearing?

9 Feb 2012 - 1:16am by Adrian (not verified)

Thanks man.

10 Feb 2012 - 4:27am by Hansu (not verified)

If i would like to covert a number from 78161519 to 78 16 15 19 how to i do? with 2 parameters (String nummber , int pairing) for example pairing 3 then instead 781 615 19

26 Mar 2012 - 8:13am by replica designer handbags (not verified)

We are a replica designer handbags leading wholesaler from China, you can wholesale knockoff handbags, cheap designer handbags, discount jewelry, discount sunglasses with a wholesale price and free shipping to US,CA,AU,UK and all over the world.

26 Mar 2012 - 11:50pm by Anonymous (not verified)

NumberFormat is a superclass of DecimalFormat, so, second line isn't wrong:
NumberFormat formatter = new DecimalFormat("000000");

But, of course, it would be clearer to write:
DecimalFormat formatter = new DecimalFormat("000000");

28 Mar 2012 - 12:33am by zain123 (not verified)

hi
i am calculating a percentage value. (var1*100)/var2 in java. the output is a 'double' number wit about 9 digits after the decimal. i am displaying this o/p on a JSP. i need only one digit after the decimal. Eg a result 53.6258412368 must be dispalyed as 53.6.

can anyone help.

15 May 2012 - 3:11pm by Anonymous (not verified)

Falla este medoto con : 556.08 * 3.0

15 May 2012 - 9:09pm by Anonymous (not verified)

I am having 2 string values like 001 and 002 if i convert into integer format again i have to get same format 001 and 002 only..
How can i do this? Can any one help.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.