Java Data Type How to - Get to know the Number format options








Question

We would like to know how to get to know the Number format options.

Answer

<p>/*  w  ww . jav a2 s . c  om*/
The Format Specifiers
</p>
<table class='table table-bordered'>
<tbody><tr><th>Format Specifier </th><th>Conversion Applied</th></tr>
<tr><td>%a %A</td><td>Floating-point hexadecimal</td></tr>
<tr><td>%b %B</td><td>Boolean</td></tr>
<tr><td>%c %C</td><td>Character</td></tr>
<tr><td>%d</td><td>Decimal integer</td></tr>
<tr><td>%h %H </td><td>Hash code of the argument</td></tr>
<tr><td>%e %E </td><td>Scientific notation</td></tr>
<tr><td>%f</td><td>Decimal floating-point</td></tr>
<tr><td>%g %G </td><td>Uses %e or %f, whichever is shorter</td></tr>
<tr><td>%o</td><td>Octal integer</td></tr>
<tr><td>%n</td><td>Inserts a newline character</td></tr>
<tr><td>%s %S </td><td>String</td></tr>
<tr><td>%t %T  </td><td>Time and date</td></tr>
<tr><td>%x %X </td><td>Integer hexadecimal</td></tr>
<tr><td>%%</td><td>Inserts a % sign</td></tr>
</tbody>
</table>
<p>
Uppercase Option
</p>
<table class='table table-bordered'>
<tbody><tr><th>Specifier</th><th>Effect</th></tr>
<tr><td>%A</td><td>Causes the hexadecimal digits a through f to be displayed in uppercase as 
                   A through F. Also, the prefix 0x is displayed as 0X, and the p will be 
                   displayed as P.</td></tr>
<tr><td>%B</td><td>Uppercases the values true and false.</td></tr>
<tr><td>%C</td><td>Uppercases the corresponding character argument.
</td></tr>
<tr><td>%E </td><td> Causes the e symbol that indicates the exponent to be displayed in uppercase.</td></tr>
<tr><td>%G</td><td>Causes the e symbol that indicates the exponent to be displayed in uppercase.</td></tr>
<tr><td>%H</td><td>Causes the hexadecimal digits a through f to be displayed in uppercase as A through F.</td></tr>
<tr><td>%S</td><td>Uppercases the corresponding string.</td></tr>
<tr><td>%T</td><td>Causes all alphabetical output to be displayed in uppercase.</td></tr>
<tr><td>%X</td><td>Causes the hexadecimal digits a through f to be displayed 
                   in uppercase as A through F. Also, the optional prefix 0x 
                   is displayed as 0X, if present.</td></tr>
</tbody>
</table>
                   

<p>
Format Flags
</p>
<table class='table table-bordered'>
<tbody><tr><th>Flag</th><th>Effect</th></tr>
<tr><td>-</td><td>Left justification</td></tr>
<tr><td>#</td><td>Alternate conversion format</td></tr>
<tr><td>0</td><td>Output is padded with zeros rather than spaces</td></tr>
<tr><td>space</td><td>Positive numeric output is preceded by a space</td></tr>
<tr><td>+</td><td> Positive numeric output is preceded by a + sign</td></tr>
<tr><td>,</td><td> Numeric values include grouping separators</td></tr>
<tr><td>(</td><td>Negative numeric values are enclosed within parentheses</td></tr>
</tbody>
</table>