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
You can format a number to a specific length like this:
System.out.format("%5s", Integer.toString(n));
@foo only if you want to print to the stdout the message.
else you can use String.format("%5s", Integer.toString(n));
Great hammer of Thor, that is poewfrully helpful!
Error on the first line of code, "NumberFormat" should be "DecimalFormat".
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 :(
@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);
hello
some people
if i want to format 123.567 as 123.57 and 700 as 700.00
what should i do?
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?
Thanks man.
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
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.
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");
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.
Falla este medoto con : 556.08 * 3.0
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.