forked from ashishjohn1908/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberformat.html
More file actions
703 lines (656 loc) · 26.1 KB
/
Copy pathnumberformat.html
File metadata and controls
703 lines (656 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Formatting Numeric Print Output (The Java™ Tutorials >
Learning the Java Language > Numbers and Strings)
</title>
<meta name="description" content="This beginner Java tutorial describes fundamentals of programming in the Java programming language" />
<meta name="keywords" content="java programming, learn java, java sample code, java objects, java classes, java inheritance, interfaces, variables, arrays, data types, operators, control flow, number, string" />
<style type="text/css">
.FigureCaption {
margin-left: 1in;
margin-right: 1in;
font-family: sans-serif;
font-size: smaller;
text-align: justify;
}
#TopBar_bl {
background: url(../../images/java_bar_bl.gif) 0 100% no-repeat;
width: 100%;
height: 60px;
}
#TopBar_br {
background: url(../../images/java_bar_br.gif) 100% 100% no-repeat;
width: 100%;
height: 60px;
}
#TopBar_tl {
background: url(../../images/java_bar_tl.gif) 0 0 no-repeat;
width: 100%;
height: 60px;
}
#TopBar_tr {
background: url(../../images/java_bar_tr.gif) 100% 0 no-repeat;
width: 100%;
height: 60px;
}
#TopBar {
background: #35556B url(../../images/java_bar.gif);
margin: 10px 10px 0 10px;
height:60px;
min-width:700px;
color: white;
font-family: sans-serif;
font-weight: bold;
}
@media print {
#BreadCrumbs, #Download {
display: none;
}
}
#TopBar_right {
line-height: 14px;
float: right;
padding-top: 2px;
padding-right: 30px;
text-align: left;
}
@media print {
#TopBar_right {
display: none;
}
}
#TopBar_right a {
font-size: 12px;
margin: 3px;
padding: 0;
}
#TopBar a:visited, #TopBar a:link {
color: white;
text-decoration: none;
}
#TopBar a:hover, #TopBar a:active {
background-color: white;
color: #35556B;
}
#BreadCrumbs {
padding: 4px 5px 0.5em 0;
font-family: sans-serif;
float: right;
}
#BreadCrumbs a {
color: blue;
}
#BreadCrumbs a:visited, #BreadCrumbs a:link {
text-decoration: none;
}
#BreadCrumbs a:hover, #BreadCrumbs a:active {
text-decoration: underline;
}
#PageTitle {
margin: 0 5px 0.5em 0;
color: #F90000;
}
#PageContent{
margin: 0 5px 0 20px;
}
.LeftBar_shown {
width: 13em;
float: left;
margin-left: 10px;
margin-top: 4px;
margin-bottom: 2em;
margin-right: 10px;
}
@media print {
.LeftBar_shown {
display: none;
}
}
.LeftBar_hidden {
display: none;
}
#Footer {
padding-top: 10px;
padding-left: 10px;
margin-right: 10px;
}
.footertext {
font-size: 10px;
font-family: sans-serif;
margin-top: 1px;
}
#Footer2 {
padding-top: 10px;
padding-left: 10px;
margin-right: 10px;
}
.NavBit {
padding: 4px 5px 0.5em 0;
font-family: sans-serif;
}
@media print {
.NavBit {
display: none;
}
}
#TagNotes {
text-align: right;
}
@media print {
#TagNotes a:visited, #TagNotes a:link {
color: #35556B;
text-decoration: none;
}
}
#Contents a, .NavBit a, #TagNotes a {
color: blue
}
#TagNotes a:visited, #TagNotes a:link,
#Contents a:visited, #Contents a:link,
.NavBit a:visited, .NavBit a:link {
text-decoration: none;
}
#TagNotes a:hover, #TagNotes a:active,
#Contents a:hover, #Contents a:active,
.NavBit a:hover, .NavBit a:active {
text-decoration: underline;
}
#Contents {
float: left;
font-family: sans-serif;
}
@media print {
#Contents {
display: none;
}
}
@media screen {
div.PrintHeaders {
display: none;
}
}
.linkLESSON, .nolinkLESSON {
margin-left: 0.5em;
text-indent: -0.5em
}
.linkAHEAD, .nolinkAHEAD, .linkQUESTIONS, .nolinkQUESTIONS {
margin-left: 1.5em;
text-indent: -0.5em
}
.linkBHEAD, .nolinkBHEAD {
margin-left: 2.5em;
text-indent: -0.5em
}
.linkCHEAD, .nolinkCHEAD {
margin-left: 3.5em;
text-indent: -0.5em
}
.nolinkLESSON, .nolinkAHEAD, .nolinkBHEAD, .nolinkCHEAD,
.nolinkQUESTIONS {
font-weight: bold;
color: #F90000;
}
.MainFlow_indented {
margin-right: 10px;
margin-left: 15em;
margin-bottom: 2em;
}
.MainFlow_wide {
margin-right: 10px;
margin-left: 10px;
margin-bottom: 2em;
}
@media print {
.MainFlow_indented, .MainFlow_wide {
padding-top: 0;
margin-top: 10px;
margin-right: 10px;
margin-left: 0;
}
}
h1, h2, h3, h4, h5 {
color: #F90000;
font-family: sans-serif;
}
h1 {
font-weight: bold;
font-size: 20px;
}
h2 {
font-weight: bold;
font-size: 17px;
}
h3 {
font-weight: bold;
font-size: 14px;
}
h4 {
font-size: 15px;
}
h5 {
font-size: 12px;
}
#ToggleLeft {
display: none;
}
.note {
margin: 0 30px 0px 30px;
}
.codeblock {
margin: 0 30px 0px 30px;
}
.tocli {
list-style-type:none;
}
.betadraft {
color: red;
}
</style>
<script type="text/javascript">
/* <![CDATA[ */
function leftBar() {
var nameq = 'tutorial_showLeftBar='
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookieString = cookies[i];
while (cookieString.charAt(0) == ' ') {
cookieString = cookieString.substring(1, cookieString.length);
}
if (cookieString.indexOf(nameq) == 0) {
cookieValue = cookieString.substring(nameq.length,
cookieString.length);
return cookieValue == 'yes';
}
}
return true;
}
function showLeft(b) {
var contents = document.getElementById("LeftBar");
var main = document.getElementById("MainFlow");
var toggle = document.getElementById("ToggleLeft");
if (b) {
contents.className = "LeftBar_shown";
main.className = "MainFlow_indented";
toggle.innerHTML = "Hide TOC";
document.cookie = 'tutorial_showLeftBar=yes; path=/';
} else {
contents.className = "LeftBar_hidden";
main.className = "MainFlow_wide";
toggle.innerHTML = "Show the TOC";
document.cookie = 'tutorial_showLeftBar=no; path=/';
}
}
function toggleLeft() {
showLeft(document.getElementById("LeftBar").className ==
"LeftBar_hidden");
document.getElementById("ToggleLeft").blur();
}
function load() {
showLeft(leftBar());
document.getElementById("ToggleLeft").style.display="inline";
}
function showCode(displayCodePage, codePath) {
var codePathEls = codePath.split("/");
var currDocPathEls = location.href.split("/");
//alert ("codePathEls = " + codePathEls + "\n" + "currDocPathEls = " + currDocPathEls);
currDocPathEls.pop(); // remove file name at the end
while (codePathEls.length > 0) {
if (codePathEls[0] == "..") {
codePathEls.shift();
currDocPathEls.pop();
} else {
break;
}
}
var fullCodePath = currDocPathEls.join("/") + "/" + codePathEls.join("/");
//alert ("fullCodePath = " + fullCodePath );
if (codePath.indexOf(".java") != -1 || codePath.indexOf(".jnlp") != -1) {
window.location.href = displayCodePage + "?code=" + encodeURI(fullCodePath);
} else {
window.location.href = fullCodePath;
}
}
/* ]]> */
</script>
</head>
<body onload="load()">
<noscript>
A browser with JavaScript enabled is required for this page to operate properly.
</noscript>
<div id="TopBar"> <div id="TopBar_tr"> <div id="TopBar_tl"> <div id="TopBar_br"> <div id="TopBar_bl">
<div id="TopBar_right">
<a target="_blank"
href="http://www.oracle.com/technetwork/java/javase/downloads/java-se-7-tutorial-2012-02-28-1536013.html">Download Ebooks</a><br />
<a target="_blank"
href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Download JDK</a>
<br />
<a href="../../search.html" target="_blank">Search Java Tutorials</a>
<br />
<a href="javascript:toggleLeft()"
id="ToggleLeft">Hide TOC</a>
</div>
</div> </div> </div> </div> </div>
<div class="PrintHeaders">
<b>Trail:</b> Learning the Java Language
<br /><b>Lesson:</b> Numbers and Strings
<br /><b>Section:</b> Numbers
</div>
<div id="LeftBar" class="LeftBar_shown">
<div id="Contents">
<div class="linkLESSON"><a href="index.html">Numbers and Strings</a></div>
<div class="linkAHEAD"><a href="numbers.html">Numbers</a></div>
<div class="linkBHEAD"><a href="numberclasses.html">The Numbers Classes</a></div>
<div class="nolinkBHEAD">Formatting Numeric Print Output</div>
<div class="linkBHEAD"><a href="beyondmath.html">Beyond Basic Arithmetic</a></div>
<div class="linkBHEAD"><a href="numbersummary.html">Summary of Numbers</a></div>
<div class="linkQUESTIONS"><a href="QandE/numbers-questions.html">Questions and Exercises</a></div>
<div class="linkAHEAD"><a href="characters.html">Characters</a></div>
<div class="linkAHEAD"><a href="strings.html">Strings</a></div>
<div class="linkBHEAD"><a href="converting.html">Converting Between Numbers and Strings</a></div>
<div class="linkBHEAD"><a href="manipstrings.html">Manipulating Characters in a String</a></div>
<div class="linkBHEAD"><a href="comparestrings.html">Comparing Strings and Portions of Strings</a></div>
<div class="linkBHEAD"><a href="buffers.html">The StringBuilder Class</a></div>
<div class="linkBHEAD"><a href="stringsummary.html">Summary of Characters and Strings</a></div>
<div class="linkAHEAD"><a href="autoboxing.html">Autoboxing and Unboxing</a></div>
<div class="linkQUESTIONS"><a href="QandE/characters-questions.html">Questions and Exercises</a></div>
</div>
</div>
<div id="MainFlow" class="MainFlow_indented">
<span id="BreadCrumbs">
<a href="../../index.html" target="_top">Home Page</a>
>
<a href="../index.html" target="_top">Learning the Java Language</a>
>
<a href="index.html" target="_top">Numbers and Strings</a>
</span>
<div class="NavBit">
<a target="_top" href="numberclasses.html">« Previous</a> • <a target="_top" href="../TOC.html">Trail</a> • <a target="_top" href="beyondmath.html">Next »</a>
</div>
<div id="PageTitle"><h1>Formatting Numeric Print Output</h1></div>
<div id="PageContent">
<!-- Formatting Numeric Print Output -->
<p>Earlier you saw the use of the <code>print</code> and <code>println</code> methods for printing strings to standard output (<code>System.out</code>). Since all numbers can be converted to strings (as you will see later in this
lesson),
you can use these methods to print out an arbitrary mixture of strings and numbers. The Java programming language has other methods, however, that allow you to exercise much more control over your print output when numbers are included.</p>
<h2>The printf and format Methods</h2>
<p>The <code>java.io</code> package includes a <code>PrintStream</code> class that has two formatting methods that you can use to replace <code>print</code> and <code>println</code>. These methods, <code>format</code> and <code>printf</code>, are equivalent to one another. The familiar <code>System.out</code> that you have been using happens to be a <code>PrintStream</code> object, so you can invoke <code>PrintStream</code> methods on <code>System.out</code>. Thus, you can use <code>format</code> or <code>printf</code> anywhere in your code where you have previously been using <code>print</code> or <code>println</code>. For example,</p>
<div class="codeblock"><pre>
System.out.format(.....);
</pre></div>
<p>The syntax for these two
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html"><code>java.io.PrintStream</code> </a> methods is the same:</p>
<div class="codeblock"><pre>
public PrintStream format(String format, Object... args)
</pre></div>
<p>where <code>format</code> is a string that specifies the formatting to be used and <code>args</code> is a list of the variables to be printed using that formatting. A simple example would be</p>
<div class="codeblock"><pre>
System.out.format("The value of " + "the float variable is " +
"%f, while the value of the " + "integer variable is %d, " +
"and the string is %s", floatVar, intVar, stringVar);
</pre></div>
<p>The first parameter, <code>format</code>, is a format string specifying how the objects in the second parameter, <code>args</code>, are to be formatted. The format string contains plain text as well as <em>format specifiers</em>, which are special characters that format the arguments of <code>Object... args</code>. (The notation <code>Object... args</code> is called <em>varargs</em>, which means that the number of arguments may vary.)</p>
<p>Format specifiers begin with a percent sign (%) and end with a <i>converter</i>. The converter is a character indicating the type of argument to be formatted. In between the percent sign (%) and the converter you can have optional flags and specifiers. There are many converters, flags, and specifiers, which are documented in
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html"><code>java.util.Formatter</code> </a></p>
<p>Here is a basic example:</p>
<div class="codeblock"><pre>
int i = 461012;
System.out.format("The value of i is: %d%n", i);
</pre></div>
<p>The <code>%d</code> specifies that the single variable is a decimal integer. The <code>%n</code> is a platform-independent newline character. The output is:</p>
<div class="codeblock"><pre>
The value of i is: 461012
</pre></div>
<p>The <code>printf</code> and <code>format</code> methods are overloaded. Each has a version with the following syntax:</p>
<div class="codeblock"><pre>
public PrintStream format(Locale l, String format, Object... args)
</pre></div>
<p>To print numbers in the French system (where a comma is used in place of the decimal place in the English representation of floating point numbers), for example, you would use:</p>
<div class="codeblock"><pre>
System.out.format(Locale.FRANCE,
"The value of the float " + "variable is %f, while the " +
"value of the integer variable " + "is %d, and the string is %s%n",
floatVar, intVar, stringVar);
</pre></div>
<h2>An Example</h2>
<p>The following table lists some of the converters and flags that are used in the sample program, <code>TestFormat.java</code>, that follows the table.</p>
<table width="70%" border="1" cellpadding="4" cellspacing="3" summary="Converters and flags that are used in the sample program TestFormat.java">
<caption style="font-weight: bold">Converters and Flags Used in <code>TestFormat.java</code></caption>
<tr>
<th id="h1" width="10%">Converter</th>
<th id="h2" width="10%">Flag</th>
<th id="h3" width="50%">Explanation</th>
</tr>
<tr>
<td headers="h1">d</td>
<td headers="h2"> </td>
<td headers="h3">A decimal integer.</td>
</tr>
<tr>
<td headers="h1">f</td>
<td headers="h2"> </td>
<td headers="h3">A float.</td>
</tr>
<tr>
<td headers="h1">n</td>
<td headers="h2"> </td>
<td headers="h3">A new line character appropriate to the platform running the application. You should always use <code>%n</code>, rather than <code>\n</code>.</td>
</tr>
<tr>
<td headers="h1">tB</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—locale-specific full name of month.</td>
</tr>
<tr>
<td headers="h1">td, te</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—2-digit day of month. td has leading zeroes as needed, te does not.</td>
</tr>
<tr>
<td headers="h1">ty, tY</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—ty = 2-digit year, tY = 4-digit year.</td>
</tr>
<tr>
<td headers="h1">tl</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—hour in 12-hour clock.</td>
</tr>
<tr>
<td headers="h1">tM</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—minutes in 2 digits, with leading zeroes as necessary.</td>
</tr>
<tr>
<td headers="h1">tp</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—locale-specific am/pm (lower case).</td>
</tr>
<tr>
<td headers="h1">tm</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—months in 2 digits, with leading zeroes as necessary.</td>
</tr>
<tr>
<td headers="h1">tD</td>
<td headers="h2"> </td>
<td headers="h3">A date & time conversion—date as %tm%td%ty</td>
</tr>
<tr>
<td headers="h1"> </td>
<td headers="h2">08</td>
<td headers="h3">Eight characters in width, with leading zeroes as necessary.</td>
</tr>
<tr>
<td headers="h1"> </td>
<td headers="h2">+</td>
<td headers="h3">Includes sign, whether positive or negative.</td>
</tr>
<tr>
<td headers="h1"> </td>
<td headers="h2">,</td>
<td headers="h3">Includes locale-specific grouping characters.</td>
</tr>
<tr>
<td headers="h1"> </td>
<td headers="h2">-</td>
<td headers="h3">Left-justified..</td>
</tr>
<tr>
<td headers="h1"> </td>
<td headers="h2">.3</td>
<td headers="h3">Three places after decimal point.</td>
</tr>
<tr>
<td headers="h1"> </td>
<td headers="h2">10.3</td>
<td headers="h3">Ten characters in width, right justified, with three places after decimal point.</td>
</tr>
</table>
<p>The following program shows some of the formatting that you can do with <code>format</code>. The output is shown within double quotes in the embedded comment:</p>
<div class="codeblock"><pre>
import java.util.Calendar;
import java.util.Locale;
public class TestFormat {
public static void main(String[] args) {
long n = 461012;
System.out.format("%d%n", n); // --> "461012"
System.out.format("%08d%n", n); // --> "00461012"
System.out.format("%+8d%n", n); // --> " +461012"
System.out.format("%,8d%n", n); // --> " 461,012"
System.out.format("%+,8d%n%n", n); // --> "+461,012"
double pi = Math.PI;
System.out.format("%f%n", pi); // --> "3.141593"
System.out.format("%.3f%n", pi); // --> "3.142"
System.out.format("%10.3f%n", pi); // --> " 3.142"
System.out.format("%-10.3f%n", pi); // --> "3.142"
System.out.format(Locale.FRANCE,
"%-10.4f%n%n", pi); // --> "3,1416"
Calendar c = Calendar.getInstance();
System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006"
System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am"
System.out.format("%tD%n", c); // --> "05/29/06"
}
}
</pre></div>
<div class="note"><hr /><strong>Note:</strong>
The discussion in this section covers just the basics of the <code>format</code> and <code>printf</code> methods. Further detail can be found in the
<a class="TutorialLink" target="_top" href="../../essential/io/formatting.html"><code>Basic I/O</code> </a> section of the Essential trail, in the "Formatting" page.<br />
Using <code>String.format</code> to create strings is covered in
<a class="TutorialLink" target="_top" href="strings.html">Strings</a>.
<hr /></div>
<h2>The DecimalFormat Class</h2>
<p>You can use the
<a class="APILink" target="_blank" href="http://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html"><code>java.text.DecimalFormat</code> </a> class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. <code>DecimalFormat</code> offers a great deal of flexibility in the formatting of numbers, but it can make your code more complex.</p>
<p>The example that follows creates a <code>DecimalFormat</code> object, <code>myFormatter</code>, by passing a pattern string to the <code>DecimalFormat</code> constructor. The <code>format()</code> method, which <code>DecimalFormat</code> inherits from <code>NumberFormat</code>, is then invoked by <code>myFormatter</code>—it accepts a <code>double</code> value as an argument and returns the formatted number in a string:</p>
<p>Here is a sample program that illustrates the use of <code>DecimalFormat</code>:</p>
<div class="codeblock"><pre>
import java.text.*;
public class DecimalFormatDemo {
static public void customFormat(String pattern, double value ) {
DecimalFormat myFormatter = new DecimalFormat(pattern);
String output = myFormatter.format(value);
System.out.println(value + " " + pattern + " " + output);
}
static public void main(String[] args) {
customFormat("###,###.###", 123456.789);
customFormat("###.##", 123456.789);
customFormat("000000.000", 123.78);
customFormat("$###,###.###", 12345.67);
}
}
</pre></div>
<p>The output is:</p>
<div class="codeblock"><pre>
123456.789 ###,###.### 123,456.789
123456.789 ###.## 123456.79
123.78 000000.000 000123.780
12345.67 $###,###.### $12,345.67
</pre></div>
<p>The following table explains each line of output.</p>
<table width="100%" border="1" cellpadding="4" cellspacing="3" summary="DecimalFormatDemo.java output">
<caption style="font-weight: bold"><code>DecimalFormat.java</code> Output</caption>
<tr>
<th id="h101">Value</th>
<th id="h102">Pattern</th>
<th id="h103">Output</th>
<th id="h104">Explanation</th>
</tr>
<tr>
<td headers="h101">123456.789</td>
<td headers="h102">###,###.###</td>
<td headers="h103">123,456.789</td>
<td headers="h104">The pound sign (#) denotes a digit, the comma is a placeholder for the grouping separator, and the period is a placeholder for the decimal separator.</td>
</tr>
<tr>
<td headers="h101">123456.789</td>
<td headers="h102">###.##</td>
<td headers="h103">123456.79</td>
<td headers="h104">The <code>value</code> has three digits to the right of the decimal point, but the <code>pattern</code> has only two. The <code>format</code> method handles this by rounding up.</td>
</tr>
<tr>
<td headers="h101">123.78</td>
<td headers="h102">000000.000</td>
<td headers="h103">000123.780</td>
<td headers="h104">The <code>pattern</code> specifies leading and trailing zeros, because the 0 character is used instead of the pound sign (#).</td>
</tr>
<tr>
<td headers="h101">12345.67</td>
<td headers="h102">$###,###.###</td>
<td headers="h103">$12,345.67</td>
<td headers="h104">The first character in the <code>pattern</code> is the dollar sign ($). Note that it immediately precedes the leftmost digit in the formatted <code>output</code>.</td>
</tr>
</table>
</div>
<div class="NavBit">
<a target="_top" href="numberclasses.html">« Previous</a>
•
<a target="_top" href="../TOC.html">Trail</a>
•
<a target="_top" href="beyondmath.html">Next »</a>
</div>
</div>
<div id="Footer2">
<hr />
<div id="TagNotes">
<p class="footertext">Problems with the examples? Try <a target="_blank"
href="../../information/run-examples.html">Compiling and Running
the Examples: FAQs</a>.
<br />
Complaints? Compliments? Suggestions? <a target="_blank"
href="http://docs.oracle.com/javase/feedback.html">Give
us your feedback</a>.
</p>
</div>
<div id="Footer">
<p class="footertext"><a name="license_info">Your use of this</a> page and all the material on pages under "The Java Tutorials" banner
is subject to these <a href="../../information/cpyr.html">legal notices</a>.
</p>
<table border="0" cellspacing="0" cellpadding="5" summary="">
<tr>
<td width="20%">
<table width="100%" border="0" cellspacing="0" cellpadding="5">
<tr>
<td headers="h201" align="center"><img id="duke" src="../../images/DukeWave.gif" width="55" height="55" alt="duke image" /></td>
<td headers="h202" align="left" valign="middle"><img id="oracle" src="../../images/logo_oracle_footer.gif" width="100" height="29" alt="Oracle logo" /></td>
</tr>
</table>
</td>
<td width="55%" valign="middle" align="center">
<p class="footertext"><a href="http://www.oracle.com/us/corporate/index.html">About Oracle</a> | <a href="http://www.oracle.com/technology/index.html">Oracle Technology Network</a> | <a href="http://www.oracle.com/us/legal/terms/index.html">Terms of Use</a></p>
</td>
<td width="25%" valign="middle" align="right">
<p class="footertext">Copyright © 1995, 2014 Oracle and/or its affiliates. All rights reserved.</p>
</td>
</tr>
</table>
</div>
</div>
<div class="PrintHeaders">
<b>Previous page:</b> The Numbers Classes
<br /><b>Next page:</b> Beyond Basic Arithmetic
</div>
</body>
</html>