DecimalFormat : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

DecimalFormat
java.text.DecimalFormat is the class for turning numbers into Strings, nicely decorated with decimal points and commas. It can also be used to parse Strings back into binary numbers. It works with a String pattern to describe the format of a number. You can use it with doubles, floats, longs and ints. DecimalFormat automatically rounds output to the displayed number of decimal places.
Pattern Characters Parsing
Forcing + Learning More
Handling Negatives Links
Post-Processing

Most Common DecimalFormat Pattern Characters

Most Common DecimalFormat Pattern Characters
Char Meaning
0 digit or lead zeroes
# digit or nothing at all. There is no pattern character to provide lead spaces for creating a fixed length pattern. It also works to the right of the decimal point. It also suppresses the decimal point to the left if there is no fractional part.
. decimal point. Actual character displayed is locale-specific, though the character you specify for the pattern is always a dot, e. g. in Sweden this will display as a comma.
, comma for grouping digits. Actual character displayed is locale-specific, though the character you specify for the pattern is always a docomma, e. g. in Sweden this will display as a period.
E exponent separator.
' used to surround decorative characters, including embedded spaces, e. g. '$'###,##0.00
; Used to separate the pattern for positive numbers from the pattern for negative numbers.
% display a ratio as a percentage. As in ##0.0%, multiplies the displayed value by 100 and displays a decorative trailing %.

Forcing +

Note there is no + pattern character to force an explicit positive sign. However, you can get that effect using decorative + and - this way:
// Getting an explicit + - sign
// Formats 3.0 as "+3.0" and -3.0 as "-3.0".
// The surrounding ' are not needed in this case.
DecimalFormat explicitSign = new DecimalFormat( "'+'0.0;'-'0.0" );

Handling Negatives

Note that - is not used to arrange for a minus sign for negative numbers. That happens automatically.

You can say something like this to arrange for negative numbers to be shown in parentheses:

new DecimalFormat ( #,##0.0#;(#,##0.0#) );

Post-Processing

If DecimalFormat won’t do exactly what you want, don’t despair. Just add your own post-processing on the String it produces. Pad to fixed length with ST. leftPad or ST.rightPad or ST.toLZ. You can download the source for ST as part of the com.mindprod.common18 package.

Parsing

You can also use DecimalFormat to parse String to extract ints, longs and doubles you can compute

Learning More

When you study Oracle’s documentation, don’t forget to look at NumberFormat, DecimalFormat’s abstract superclass. That’s where most of its useful methods are. DecimalFormat documents the magic characters you can use in pattern strings.
Oracle’s Javadoc on DecimalFormat class : available:
Oracle’s Javadoc on NumberFormat class : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/decimalformat.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\decimalformat.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.144.40.239]
You are visitor number