src/share/classes/java/util/Formatter.java

Print this page
rev 7582 : 8019857: Fix doclint errors in java.util.Format*
Summary: Fix doclint errors in java.util.Format*.
Reviewed-by: TBD
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>


 824  * {@link IllegalFormatPrecisionException} will be thrown.
 825  *
 826  * <h4><a name="dnum">Numeric</a></h4>
 827  *
 828  * <p> Numeric conversions are divided into the following categories:
 829  *
 830  * <ol>
 831  *
 832  * <li> <a href="#dnint"><b>Byte, Short, Integer, and Long</b></a>
 833  *
 834  * <li> <a href="#dnbint"><b>BigInteger</b></a>
 835  *
 836  * <li> <a href="#dndec"><b>Float and Double</b></a>
 837  *
 838  * <li> <a href="#dnbdec"><b>BigDecimal</b></a>
 839  *
 840  * </ol>
 841  *
 842  * <p> Numeric types will be formatted according to the following algorithm:
 843  *
 844  * <p><b><a name="l10n algorithm"> Number Localization Algorithm</a></b>
 845  *
 846  * <p> After digits are obtained for the integer part, fractional part, and
 847  * exponent (as appropriate for the data type), the following transformation
 848  * is applied:
 849  *
 850  * <ol>
 851  *
 852  * <li> Each digit character <i>d</i> in the string is replaced by a
 853  * locale-specific digit computed relative to the current locale's
 854  * {@linkplain java.text.DecimalFormatSymbols#getZeroDigit() zero digit}
 855  * <i>z</i>; that is <i>d&nbsp;-&nbsp;</i> {@code '0'}
 856  * <i>&nbsp;+&nbsp;z</i>.
 857  *
 858  * <li> If a decimal separator is present, a locale-specific {@linkplain
 859  * java.text.DecimalFormatSymbols#getDecimalSeparator decimal separator} is
 860  * substituted.
 861  *
 862  * <li> If the {@code ','} (<tt>'&#92;u002c'</tt>)
 863  * <a name="l10n group">flag</a> is given, then the locale-specific {@linkplain
 864  * java.text.DecimalFormatSymbols#getGroupingSeparator grouping separator} is
 865  * inserted by scanning the integer part of the string from least significant
 866  * to most significant digits and inserting a separator at intervals defined by
 867  * the locale's {@linkplain java.text.DecimalFormat#getGroupingSize() grouping
 868  * size}.
 869  *
 870  * <li> If the {@code '0'} flag is given, then the locale-specific {@linkplain
 871  * java.text.DecimalFormatSymbols#getZeroDigit() zero digits} are inserted
 872  * after the sign character, if any, and before the first non-zero digit, until
 873  * the length of the string is equal to the requested field width.
 874  *
 875  * <li> If the value is negative and the {@code '('} flag is given, then a
 876  * {@code '('} (<tt>'&#92;u0028'</tt>) is prepended and a {@code ')'}
 877  * (<tt>'&#92;u0029'</tt>) is appended.
 878  *
 879  * <li> If the value is negative (or floating-point negative zero) and
 880  * {@code '('} flag is not given, then a {@code '-'} (<tt>'&#92;u002d'</tt>)
 881  * is prepended.
 882  *
 883  * <li> If the {@code '+'} flag is given and the value is positive or zero (or


 885  * will be prepended.
 886  *
 887  * </ol>
 888  *
 889  * <p> If the value is NaN or positive infinity the literal strings "NaN" or
 890  * "Infinity" respectively, will be output.  If the value is negative infinity,
 891  * then the output will be "(Infinity)" if the {@code '('} flag is given
 892  * otherwise the output will be "-Infinity".  These values are not localized.
 893  *
 894  * <p><a name="dnint"><b> Byte, Short, Integer, and Long </b></a>
 895  *
 896  * <p> The following conversions may be applied to {@code byte}, {@link Byte},
 897  * {@code short}, {@link Short}, {@code int} and {@link Integer},
 898  * {@code long}, and {@link Long}.
 899  *
 900  * <table cellpadding=5 summary="IntConv">
 901  *
 902  * <tr><td valign="top"> {@code 'd'}
 903  *     <td valign="top"> <tt>'&#92;u0054'</tt>
 904  *     <td> Formats the argument as a decimal integer. The <a
 905  *     href="#l10n algorithm">localization algorithm</a> is applied.
 906  *
 907  *     <p> If the {@code '0'} flag is given and the value is negative, then
 908  *     the zero padding will occur after the sign.
 909  *
 910  *     <p> If the {@code '#'} flag is given then a {@link
 911  *     FormatFlagsConversionMismatchException} will be thrown.
 912  *
 913  * <tr><td valign="top"> {@code 'o'}
 914  *     <td valign="top"> <tt>'&#92;u006f'</tt>
 915  *     <td> Formats the argument as an integer in base eight.  No localization
 916  *     is applied.
 917  *
 918  *     <p> If <i>x</i> is negative then the result will be an unsigned value
 919  *     generated by adding 2<sup>n</sup> to the value where {@code n} is the
 920  *     number of bits in the type as returned by the static {@code SIZE} field
 921  *     in the {@linkplain Byte#SIZE Byte}, {@linkplain Short#SIZE Short},
 922  *     {@linkplain Integer#SIZE Integer}, or {@linkplain Long#SIZE Long}
 923  *     classes as appropriate.
 924  *
 925  *     <p> If the {@code '#'} flag is given then the output will always begin


 994  *     (<tt>'&#92;u0020'</tt>) for non-negative values.
 995  *
 996  *     <p> If both the {@code '+'} and <tt>'&nbsp;&nbsp;'</tt> flags are given
 997  *     then an {@link IllegalFormatFlagsException} will be thrown.
 998  *
 999  * <tr><td valign="top"> {@code '0'}
1000  *     <td valign="top"> <tt>'&#92;u0030'</tt>
1001  *     <td> Requires the output to be padded with leading {@linkplain
1002  *     java.text.DecimalFormatSymbols#getZeroDigit zeros} to the minimum field
1003  *     width following any sign or radix indicator except when converting NaN
1004  *     or infinity.  If the width is not provided, then a {@link
1005  *     MissingFormatWidthException} will be thrown.
1006  *
1007  *     <p> If both the {@code '-'} and {@code '0'} flags are given then an
1008  *     {@link IllegalFormatFlagsException} will be thrown.
1009  *
1010  * <tr><td valign="top"> {@code ','}
1011  *     <td valign="top"> <tt>'&#92;u002c'</tt>
1012  *     <td> Requires the output to include the locale-specific {@linkplain
1013  *     java.text.DecimalFormatSymbols#getGroupingSeparator group separators} as
1014  *     described in the <a href="#l10n group">"group" section</a> of the
1015  *     localization algorithm.
1016  *
1017  * <tr><td valign="top"> {@code '('}
1018  *     <td valign="top"> <tt>'&#92;u0028'</tt>
1019  *     <td> Requires the output to prepend a {@code '('}
1020  *     (<tt>'&#92;u0028'</tt>) and append a {@code ')'}
1021  *     (<tt>'&#92;u0029'</tt>) to negative values.
1022  *
1023  * </table>
1024  *
1025  * <p> If no <a name="intdFlags">flags</a> are given the default formatting is
1026  * as follows:
1027  *
1028  * <ul>
1029  *
1030  * <li> The output is right-justified within the {@code width}
1031  *
1032  * <li> Negative numbers begin with a {@code '-'} (<tt>'&#92;u002d'</tt>)
1033  *
1034  * <li> Positive numbers and zero do not include a sign or extra leading


1043  * separators, radix indicator, and parentheses.  If the length of the
1044  * converted value is less than the width then the output will be padded by
1045  * spaces (<tt>'&#92;u0020'</tt>) until the total number of characters equals
1046  * width.  The padding is on the left by default.  If {@code '-'} flag is
1047  * given then the padding will be on the right.  If width is not specified then
1048  * there is no minimum.
1049  *
1050  * <p> The precision is not applicable.  If precision is specified then an
1051  * {@link IllegalFormatPrecisionException} will be thrown.
1052  *
1053  * <p><a name="dnbint"><b> BigInteger </b></a>
1054  *
1055  * <p> The following conversions may be applied to {@link
1056  * java.math.BigInteger}.
1057  *
1058  * <table cellpadding=5 summary="BIntConv">
1059  *
1060  * <tr><td valign="top"> {@code 'd'}
1061  *     <td valign="top"> <tt>'&#92;u0054'</tt>
1062  *     <td> Requires the output to be formatted as a decimal integer. The <a
1063  *     href="#l10n algorithm">localization algorithm</a> is applied.
1064  *
1065  *     <p> If the {@code '#'} flag is given {@link
1066  *     FormatFlagsConversionMismatchException} will be thrown.
1067  *
1068  * <tr><td valign="top"> {@code 'o'}
1069  *     <td valign="top"> <tt>'&#92;u006f'</tt>
1070  *     <td> Requires the output to be formatted as an integer in base eight.
1071  *     No localization is applied.
1072  *
1073  *     <p> If <i>x</i> is negative then the result will be a signed value
1074  *     beginning with {@code '-'} (<tt>'&#92;u002d'</tt>).  Signed output is
1075  *     allowed for this type because unlike the primitive types it is not
1076  *     possible to create an unsigned equivalent without assuming an explicit
1077  *     data-type size.
1078  *
1079  *     <p> If <i>x</i> is positive or zero and the {@code '+'} flag is given
1080  *     then the result will begin with {@code '+'} (<tt>'&#92;u002b'</tt>).
1081  *
1082  *     <p> If the {@code '#'} flag is given then the output will always begin
1083  *     with {@code '0'} prefix.


1138  * Long apply.  The <a href="#intdFlags">default behavior</a> when no flags are
1139  * given is the same as for Byte, Short, Integer, and Long.
1140  *
1141  * <p> The specification of <a href="#intWidth">width</a> is the same as
1142  * defined for Byte, Short, Integer, and Long.
1143  *
1144  * <p> The precision is not applicable.  If precision is specified then an
1145  * {@link IllegalFormatPrecisionException} will be thrown.
1146  *
1147  * <p><a name="dndec"><b> Float and Double</b></a>
1148  *
1149  * <p> The following conversions may be applied to {@code float}, {@link
1150  * Float}, {@code double} and {@link Double}.
1151  *
1152  * <table cellpadding=5 summary="floatConv">
1153  *
1154  * <tr><td valign="top"> {@code 'e'}
1155  *     <td valign="top"> <tt>'&#92;u0065'</tt>
1156  *     <td> Requires the output to be formatted using <a
1157  *     name="scientific">computerized scientific notation</a>.  The <a
1158  *     href="#l10n algorithm">localization algorithm</a> is applied.
1159  *
1160  *     <p> The formatting of the magnitude <i>m</i> depends upon its value.
1161  *
1162  *     <p> If <i>m</i> is NaN or infinite, the literal strings "NaN" or
1163  *     "Infinity", respectively, will be output.  These values are not
1164  *     localized.
1165  *
1166  *     <p> If <i>m</i> is positive-zero or negative-zero, then the exponent
1167  *     will be {@code "+00"}.
1168  *
1169  *     <p> Otherwise, the result is a string that represents the sign and
1170  *     magnitude (absolute value) of the argument.  The formatting of the sign
1171  *     is described in the <a href="#l10n algorithm">localization
1172  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1173  *     value.
1174  *
1175  *     <p> Let <i>n</i> be the unique integer such that 10<sup><i>n</i></sup>
1176  *     &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
1177  *     mathematically exact quotient of <i>m</i> and 10<sup><i>n</i></sup> so
1178  *     that 1 &lt;= <i>a</i> &lt; 10. The magnitude is then represented as the
1179  *     integer part of <i>a</i>, as a single decimal digit, followed by the
1180  *     decimal separator followed by decimal digits representing the fractional
1181  *     part of <i>a</i>, followed by the exponent symbol {@code 'e'}
1182  *     (<tt>'&#92;u0065'</tt>), followed by the sign of the exponent, followed
1183  *     by a representation of <i>n</i> as a decimal integer, as produced by the
1184  *     method {@link Long#toString(long, int)}, and zero-padded to include at
1185  *     least two digits.
1186  *
1187  *     <p> The number of digits in the result for the fractional part of
1188  *     <i>m</i> or <i>a</i> is equal to the precision.  If the precision is not
1189  *     specified then the default value is {@code 6}. If the precision is less
1190  *     than the number of digits which would appear after the decimal point in
1191  *     the string returned by {@link Float#toString(float)} or {@link
1192  *     Double#toString(double)} respectively, then the value will be rounded
1193  *     using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1194  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1195  *     For a canonical representation of the value, use {@link
1196  *     Float#toString(float)} or {@link Double#toString(double)} as
1197  *     appropriate.
1198  *
1199  *     <p>If the {@code ','} flag is given, then an {@link
1200  *     FormatFlagsConversionMismatchException} will be thrown.
1201  *
1202  * <tr><td valign="top"> {@code 'E'}
1203  *     <td valign="top"> <tt>'&#92;u0045'</tt>
1204  *     <td> The upper-case variant of {@code 'e'}.  The exponent symbol
1205  *     will be {@code 'E'} (<tt>'&#92;u0045'</tt>).
1206  *
1207  * <tr><td valign="top"> {@code 'g'}
1208  *     <td valign="top"> <tt>'&#92;u0067'</tt>
1209  *     <td> Requires the output to be formatted in general scientific notation
1210  *     as described below. The <a href="#l10n algorithm">localization
1211  *     algorithm</a> is applied.
1212  *
1213  *     <p> After rounding for the precision, the formatting of the resulting
1214  *     magnitude <i>m</i> depends on its value.
1215  *
1216  *     <p> If <i>m</i> is greater than or equal to 10<sup>-4</sup> but less
1217  *     than 10<sup>precision</sup> then it is represented in <i><a
1218  *     href="#decimal">decimal format</a></i>.
1219  *
1220  *     <p> If <i>m</i> is less than 10<sup>-4</sup> or greater than or equal to
1221  *     10<sup>precision</sup>, then it is represented in <i><a
1222  *     href="#scientific">computerized scientific notation</a></i>.
1223  *
1224  *     <p> The total number of significant digits in <i>m</i> is equal to the
1225  *     precision.  If the precision is not specified, then the default value is
1226  *     {@code 6}.  If the precision is {@code 0}, then it is taken to be
1227  *     {@code 1}.
1228  *
1229  *     <p> If the {@code '#'} flag is given then an {@link
1230  *     FormatFlagsConversionMismatchException} will be thrown.
1231  *
1232  * <tr><td valign="top"> {@code 'G'}
1233  *     <td valign="top"> <tt>'&#92;u0047'</tt>
1234  *     <td> The upper-case variant of {@code 'g'}.
1235  *
1236  * <tr><td valign="top"> {@code 'f'}
1237  *     <td valign="top"> <tt>'&#92;u0066'</tt>
1238  *     <td> Requires the output to be formatted using <a name="decimal">decimal
1239  *     format</a>.  The <a href="#l10n algorithm">localization algorithm</a> is
1240  *     applied.
1241  *
1242  *     <p> The result is a string that represents the sign and magnitude
1243  *     (absolute value) of the argument.  The formatting of the sign is
1244  *     described in the <a href="#l10n algorithm">localization
1245  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1246  *     value.
1247  *
1248  *     <p> If <i>m</i> NaN or infinite, the literal strings "NaN" or
1249  *     "Infinity", respectively, will be output.  These values are not
1250  *     localized.
1251  *
1252  *     <p> The magnitude is formatted as the integer part of <i>m</i>, with no
1253  *     leading zeroes, followed by the decimal separator followed by one or
1254  *     more decimal digits representing the fractional part of <i>m</i>.
1255  *
1256  *     <p> The number of digits in the result for the fractional part of
1257  *     <i>m</i> or <i>a</i> is equal to the precision.  If the precision is not
1258  *     specified then the default value is {@code 6}. If the precision is less
1259  *     than the number of digits which would appear after the decimal point in
1260  *     the string returned by {@link Float#toString(float)} or {@link
1261  *     Double#toString(double)} respectively, then the value will be rounded
1262  *     using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1263  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1264  *     For a canonical representation of the value, use {@link


1365  * rounding.  If the precision is not specified, then the default value is
1366  * {@code 6}.  If the precision is {@code 0}, then it is taken to be
1367  * {@code 1}.
1368  *
1369  * <p> If the conversion is {@code 'a'} or {@code 'A'}, then the precision
1370  * is the number of hexadecimal digits after the decimal separator.  If the
1371  * precision is not provided, then all of the digits as returned by {@link
1372  * Double#toHexString(double)} will be output.
1373  *
1374  * <p><a name="dnbdec"><b> BigDecimal </b></a>
1375  *
1376  * <p> The following conversions may be applied {@link java.math.BigDecimal
1377  * BigDecimal}.
1378  *
1379  * <table cellpadding=5 summary="floatConv">
1380  *
1381  * <tr><td valign="top"> {@code 'e'}
1382  *     <td valign="top"> <tt>'&#92;u0065'</tt>
1383  *     <td> Requires the output to be formatted using <a
1384  *     name="bscientific">computerized scientific notation</a>.  The <a
1385  *     href="#l10n algorithm">localization algorithm</a> is applied.
1386  *
1387  *     <p> The formatting of the magnitude <i>m</i> depends upon its value.
1388  *
1389  *     <p> If <i>m</i> is positive-zero or negative-zero, then the exponent
1390  *     will be {@code "+00"}.
1391  *
1392  *     <p> Otherwise, the result is a string that represents the sign and
1393  *     magnitude (absolute value) of the argument.  The formatting of the sign
1394  *     is described in the <a href="#l10n algorithm">localization
1395  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1396  *     value.
1397  *
1398  *     <p> Let <i>n</i> be the unique integer such that 10<sup><i>n</i></sup>
1399  *     &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
1400  *     mathematically exact quotient of <i>m</i> and 10<sup><i>n</i></sup> so
1401  *     that 1 &lt;= <i>a</i> &lt; 10. The magnitude is then represented as the
1402  *     integer part of <i>a</i>, as a single decimal digit, followed by the
1403  *     decimal separator followed by decimal digits representing the fractional
1404  *     part of <i>a</i>, followed by the exponent symbol {@code 'e'}
1405  *     (<tt>'&#92;u0065'</tt>), followed by the sign of the exponent, followed
1406  *     by a representation of <i>n</i> as a decimal integer, as produced by the
1407  *     method {@link Long#toString(long, int)}, and zero-padded to include at
1408  *     least two digits.
1409  *
1410  *     <p> The number of digits in the result for the fractional part of
1411  *     <i>m</i> or <i>a</i> is equal to the precision.  If the precision is not
1412  *     specified then the default value is {@code 6}.  If the precision is
1413  *     less than the number of digits to the right of the decimal point then
1414  *     the value will be rounded using the
1415  *     {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1416  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1417  *     For a canonical representation of the value, use {@link
1418  *     BigDecimal#toString()}.
1419  *
1420  *     <p> If the {@code ','} flag is given, then an {@link
1421  *     FormatFlagsConversionMismatchException} will be thrown.
1422  *
1423  * <tr><td valign="top"> {@code 'E'}
1424  *     <td valign="top"> <tt>'&#92;u0045'</tt>
1425  *     <td> The upper-case variant of {@code 'e'}.  The exponent symbol
1426  *     will be {@code 'E'} (<tt>'&#92;u0045'</tt>).
1427  *
1428  * <tr><td valign="top"> {@code 'g'}
1429  *     <td valign="top"> <tt>'&#92;u0067'</tt>
1430  *     <td> Requires the output to be formatted in general scientific notation
1431  *     as described below. The <a href="#l10n algorithm">localization
1432  *     algorithm</a> is applied.
1433  *
1434  *     <p> After rounding for the precision, the formatting of the resulting
1435  *     magnitude <i>m</i> depends on its value.
1436  *
1437  *     <p> If <i>m</i> is greater than or equal to 10<sup>-4</sup> but less
1438  *     than 10<sup>precision</sup> then it is represented in <i><a
1439  *     href="#bdecimal">decimal format</a></i>.
1440  *
1441  *     <p> If <i>m</i> is less than 10<sup>-4</sup> or greater than or equal to
1442  *     10<sup>precision</sup>, then it is represented in <i><a
1443  *     href="#bscientific">computerized scientific notation</a></i>.
1444  *
1445  *     <p> The total number of significant digits in <i>m</i> is equal to the
1446  *     precision.  If the precision is not specified, then the default value is
1447  *     {@code 6}.  If the precision is {@code 0}, then it is taken to be
1448  *     {@code 1}.
1449  *
1450  *     <p> If the {@code '#'} flag is given then an {@link
1451  *     FormatFlagsConversionMismatchException} will be thrown.
1452  *
1453  * <tr><td valign="top"> {@code 'G'}
1454  *     <td valign="top"> <tt>'&#92;u0047'</tt>
1455  *     <td> The upper-case variant of {@code 'g'}.
1456  *
1457  * <tr><td valign="top"> {@code 'f'}
1458  *     <td valign="top"> <tt>'&#92;u0066'</tt>
1459  *     <td> Requires the output to be formatted using <a name="bdecimal">decimal
1460  *     format</a>.  The <a href="#l10n algorithm">localization algorithm</a> is
1461  *     applied.
1462  *
1463  *     <p> The result is a string that represents the sign and magnitude
1464  *     (absolute value) of the argument.  The formatting of the sign is
1465  *     described in the <a href="#l10n algorithm">localization
1466  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1467  *     value.
1468  *
1469  *     <p> The magnitude is formatted as the integer part of <i>m</i>, with no
1470  *     leading zeroes, followed by the decimal separator followed by one or
1471  *     more decimal digits representing the fractional part of <i>m</i>.
1472  *
1473  *     <p> The number of digits in the result for the fractional part of
1474  *     <i>m</i> or <i>a</i> is equal to the precision. If the precision is not
1475  *     specified then the default value is {@code 6}.  If the precision is
1476  *     less than the number of digits to the right of the decimal point
1477  *     then the value will be rounded using the
1478  *     {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1479  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1480  *     For a canonical representation of the value, use {@link
1481  *     BigDecimal#toString()}.
1482  *
1483  * </table>
1484  *
1485  * <p> All <a href="#intFlags">flags</a> defined for Byte, Short, Integer, and


1704  * <tr><td valign="top">{@code 'D'}
1705  *     <td valign="top"> <tt>'&#92;u0044'</tt>
1706  *     <td> Date formatted as {@code "%tm/%td/%ty"}.
1707  *
1708  * <tr><td valign="top">{@code 'F'}
1709  *     <td valign="top"> <tt>'&#92;u0046'</tt>
1710  *     <td> <a href="http://www.w3.org/TR/NOTE-datetime">ISO&nbsp;8601</a>
1711  *     complete date formatted as {@code "%tY-%tm-%td"}.
1712  *
1713  * <tr><td valign="top">{@code 'c'}
1714  *     <td valign="top"> <tt>'&#92;u0063'</tt>
1715  *     <td> Date and time formatted as {@code "%ta %tb %td %tT %tZ %tY"},
1716  *     e.g. {@code "Sun Jul 20 16:17:00 EDT 1969"}.
1717  *
1718  * </table>
1719  *
1720  * <p> The {@code '-'} flag defined for <a href="#dFlags">General
1721  * conversions</a> applies.  If the {@code '#'} flag is given, then a {@link
1722  * FormatFlagsConversionMismatchException} will be thrown.
1723  *
1724  * <p> The <a name="dtWidth">width</a> is the minimum number of characters to
1725  * be written to the output.  If the length of the converted value is less than
1726  * the {@code width} then the output will be padded by spaces
1727  * (<tt>'&#92;u0020'</tt>) until the total number of characters equals width.
1728  * The padding is on the left by default.  If the {@code '-'} flag is given
1729  * then the padding will be on the right.  If width is not specified then there
1730  * is no minimum.
1731  *
1732  * <p> The precision is not applicable.  If the precision is specified then an
1733  * {@link IllegalFormatPrecisionException} will be thrown.
1734  *
1735  * <h4><a name="dper">Percent</a></h4>
1736  *
1737  * <p> The conversion does not correspond to any argument.
1738  *
1739  * <table cellpadding=5 summary="DTConv">
1740  *
1741  * <tr><td valign="top">{@code '%'}
1742  *     <td> The result is a literal {@code '%'} (<tt>'&#92;u0025'</tt>)
1743  *
1744  * <p> The <a name="dtWidth">width</a> is the minimum number of characters to
1745  * be written to the output including the {@code '%'}.  If the length of the
1746  * converted value is less than the {@code width} then the output will be
1747  * padded by spaces (<tt>'&#92;u0020'</tt>) until the total number of
1748  * characters equals width.  The padding is on the left.  If width is not
1749  * specified then just the {@code '%'} is output.
1750  *
1751  * <p> The {@code '-'} flag defined for <a href="#dFlags">General
1752  * conversions</a> applies.  If any other flags are provided, then a
1753  * {@link FormatFlagsConversionMismatchException} will be thrown.
1754  *
1755  * <p> The precision is not applicable.  If the precision is specified an
1756  * {@link IllegalFormatPrecisionException} will be thrown.
1757  *
1758  * </table>
1759  *
1760  * <h4><a name="dls">Line Separator</a></h4>
1761  *
1762  * <p> The conversion does not correspond to any argument.
1763  *
1764  * <table cellpadding=5 summary="DTConv">


2573                 throw new UnknownFormatConversionException(String.valueOf(c));
2574             }
2575         }
2576     }
2577 
2578     private interface FormatString {
2579         int index();
2580         void print(Object arg, Locale l) throws IOException;
2581         String toString();
2582     }
2583 
2584     private class FixedString implements FormatString {
2585         private String s;
2586         FixedString(String s) { this.s = s; }
2587         public int index() { return -2; }
2588         public void print(Object arg, Locale l)
2589             throws IOException { a.append(s); }
2590         public String toString() { return s; }
2591     }
2592 
2593     public enum BigDecimalLayoutForm { SCIENTIFIC, DECIMAL_FLOAT };













2594 
2595     private class FormatSpecifier implements FormatString {
2596         private int index = -1;
2597         private Flags f = Flags.NONE;
2598         private int width;
2599         private int precision;
2600         private boolean dt = false;
2601         private char c;
2602 
2603         private int index(String s) {
2604             if (s != null) {
2605                 try {
2606                     index = Integer.parseInt(s.substring(0, s.length() - 1));
2607                 } catch (NumberFormatException x) {
2608                     assert(false);
2609                 }
2610             } else {
2611                 index = 0;
2612             }
2613             return index;




 824  * {@link IllegalFormatPrecisionException} will be thrown.
 825  *
 826  * <h4><a name="dnum">Numeric</a></h4>
 827  *
 828  * <p> Numeric conversions are divided into the following categories:
 829  *
 830  * <ol>
 831  *
 832  * <li> <a href="#dnint"><b>Byte, Short, Integer, and Long</b></a>
 833  *
 834  * <li> <a href="#dnbint"><b>BigInteger</b></a>
 835  *
 836  * <li> <a href="#dndec"><b>Float and Double</b></a>
 837  *
 838  * <li> <a href="#dnbdec"><b>BigDecimal</b></a>
 839  *
 840  * </ol>
 841  *
 842  * <p> Numeric types will be formatted according to the following algorithm:
 843  *
 844  * <p><b><a name="L10nAlgorithm"> Number Localization Algorithm</a></b>
 845  *
 846  * <p> After digits are obtained for the integer part, fractional part, and
 847  * exponent (as appropriate for the data type), the following transformation
 848  * is applied:
 849  *
 850  * <ol>
 851  *
 852  * <li> Each digit character <i>d</i> in the string is replaced by a
 853  * locale-specific digit computed relative to the current locale's
 854  * {@linkplain java.text.DecimalFormatSymbols#getZeroDigit() zero digit}
 855  * <i>z</i>; that is <i>d&nbsp;-&nbsp;</i> {@code '0'}
 856  * <i>&nbsp;+&nbsp;z</i>.
 857  *
 858  * <li> If a decimal separator is present, a locale-specific {@linkplain
 859  * java.text.DecimalFormatSymbols#getDecimalSeparator decimal separator} is
 860  * substituted.
 861  *
 862  * <li> If the {@code ','} (<tt>'&#92;u002c'</tt>)
 863  * <a name="L10nGroup">flag</a> is given, then the locale-specific {@linkplain
 864  * java.text.DecimalFormatSymbols#getGroupingSeparator grouping separator} is
 865  * inserted by scanning the integer part of the string from least significant
 866  * to most significant digits and inserting a separator at intervals defined by
 867  * the locale's {@linkplain java.text.DecimalFormat#getGroupingSize() grouping
 868  * size}.
 869  *
 870  * <li> If the {@code '0'} flag is given, then the locale-specific {@linkplain
 871  * java.text.DecimalFormatSymbols#getZeroDigit() zero digits} are inserted
 872  * after the sign character, if any, and before the first non-zero digit, until
 873  * the length of the string is equal to the requested field width.
 874  *
 875  * <li> If the value is negative and the {@code '('} flag is given, then a
 876  * {@code '('} (<tt>'&#92;u0028'</tt>) is prepended and a {@code ')'}
 877  * (<tt>'&#92;u0029'</tt>) is appended.
 878  *
 879  * <li> If the value is negative (or floating-point negative zero) and
 880  * {@code '('} flag is not given, then a {@code '-'} (<tt>'&#92;u002d'</tt>)
 881  * is prepended.
 882  *
 883  * <li> If the {@code '+'} flag is given and the value is positive or zero (or


 885  * will be prepended.
 886  *
 887  * </ol>
 888  *
 889  * <p> If the value is NaN or positive infinity the literal strings "NaN" or
 890  * "Infinity" respectively, will be output.  If the value is negative infinity,
 891  * then the output will be "(Infinity)" if the {@code '('} flag is given
 892  * otherwise the output will be "-Infinity".  These values are not localized.
 893  *
 894  * <p><a name="dnint"><b> Byte, Short, Integer, and Long </b></a>
 895  *
 896  * <p> The following conversions may be applied to {@code byte}, {@link Byte},
 897  * {@code short}, {@link Short}, {@code int} and {@link Integer},
 898  * {@code long}, and {@link Long}.
 899  *
 900  * <table cellpadding=5 summary="IntConv">
 901  *
 902  * <tr><td valign="top"> {@code 'd'}
 903  *     <td valign="top"> <tt>'&#92;u0054'</tt>
 904  *     <td> Formats the argument as a decimal integer. The <a
 905  *     href="#L10nAlgorithm">localization algorithm</a> is applied.
 906  *
 907  *     <p> If the {@code '0'} flag is given and the value is negative, then
 908  *     the zero padding will occur after the sign.
 909  *
 910  *     <p> If the {@code '#'} flag is given then a {@link
 911  *     FormatFlagsConversionMismatchException} will be thrown.
 912  *
 913  * <tr><td valign="top"> {@code 'o'}
 914  *     <td valign="top"> <tt>'&#92;u006f'</tt>
 915  *     <td> Formats the argument as an integer in base eight.  No localization
 916  *     is applied.
 917  *
 918  *     <p> If <i>x</i> is negative then the result will be an unsigned value
 919  *     generated by adding 2<sup>n</sup> to the value where {@code n} is the
 920  *     number of bits in the type as returned by the static {@code SIZE} field
 921  *     in the {@linkplain Byte#SIZE Byte}, {@linkplain Short#SIZE Short},
 922  *     {@linkplain Integer#SIZE Integer}, or {@linkplain Long#SIZE Long}
 923  *     classes as appropriate.
 924  *
 925  *     <p> If the {@code '#'} flag is given then the output will always begin


 994  *     (<tt>'&#92;u0020'</tt>) for non-negative values.
 995  *
 996  *     <p> If both the {@code '+'} and <tt>'&nbsp;&nbsp;'</tt> flags are given
 997  *     then an {@link IllegalFormatFlagsException} will be thrown.
 998  *
 999  * <tr><td valign="top"> {@code '0'}
1000  *     <td valign="top"> <tt>'&#92;u0030'</tt>
1001  *     <td> Requires the output to be padded with leading {@linkplain
1002  *     java.text.DecimalFormatSymbols#getZeroDigit zeros} to the minimum field
1003  *     width following any sign or radix indicator except when converting NaN
1004  *     or infinity.  If the width is not provided, then a {@link
1005  *     MissingFormatWidthException} will be thrown.
1006  *
1007  *     <p> If both the {@code '-'} and {@code '0'} flags are given then an
1008  *     {@link IllegalFormatFlagsException} will be thrown.
1009  *
1010  * <tr><td valign="top"> {@code ','}
1011  *     <td valign="top"> <tt>'&#92;u002c'</tt>
1012  *     <td> Requires the output to include the locale-specific {@linkplain
1013  *     java.text.DecimalFormatSymbols#getGroupingSeparator group separators} as
1014  *     described in the <a href="#L10nGroup">"group" section</a> of the
1015  *     localization algorithm.
1016  *
1017  * <tr><td valign="top"> {@code '('}
1018  *     <td valign="top"> <tt>'&#92;u0028'</tt>
1019  *     <td> Requires the output to prepend a {@code '('}
1020  *     (<tt>'&#92;u0028'</tt>) and append a {@code ')'}
1021  *     (<tt>'&#92;u0029'</tt>) to negative values.
1022  *
1023  * </table>
1024  *
1025  * <p> If no <a name="intdFlags">flags</a> are given the default formatting is
1026  * as follows:
1027  *
1028  * <ul>
1029  *
1030  * <li> The output is right-justified within the {@code width}
1031  *
1032  * <li> Negative numbers begin with a {@code '-'} (<tt>'&#92;u002d'</tt>)
1033  *
1034  * <li> Positive numbers and zero do not include a sign or extra leading


1043  * separators, radix indicator, and parentheses.  If the length of the
1044  * converted value is less than the width then the output will be padded by
1045  * spaces (<tt>'&#92;u0020'</tt>) until the total number of characters equals
1046  * width.  The padding is on the left by default.  If {@code '-'} flag is
1047  * given then the padding will be on the right.  If width is not specified then
1048  * there is no minimum.
1049  *
1050  * <p> The precision is not applicable.  If precision is specified then an
1051  * {@link IllegalFormatPrecisionException} will be thrown.
1052  *
1053  * <p><a name="dnbint"><b> BigInteger </b></a>
1054  *
1055  * <p> The following conversions may be applied to {@link
1056  * java.math.BigInteger}.
1057  *
1058  * <table cellpadding=5 summary="BIntConv">
1059  *
1060  * <tr><td valign="top"> {@code 'd'}
1061  *     <td valign="top"> <tt>'&#92;u0054'</tt>
1062  *     <td> Requires the output to be formatted as a decimal integer. The <a
1063  *     href="#L10nAlgorithm">localization algorithm</a> is applied.
1064  *
1065  *     <p> If the {@code '#'} flag is given {@link
1066  *     FormatFlagsConversionMismatchException} will be thrown.
1067  *
1068  * <tr><td valign="top"> {@code 'o'}
1069  *     <td valign="top"> <tt>'&#92;u006f'</tt>
1070  *     <td> Requires the output to be formatted as an integer in base eight.
1071  *     No localization is applied.
1072  *
1073  *     <p> If <i>x</i> is negative then the result will be a signed value
1074  *     beginning with {@code '-'} (<tt>'&#92;u002d'</tt>).  Signed output is
1075  *     allowed for this type because unlike the primitive types it is not
1076  *     possible to create an unsigned equivalent without assuming an explicit
1077  *     data-type size.
1078  *
1079  *     <p> If <i>x</i> is positive or zero and the {@code '+'} flag is given
1080  *     then the result will begin with {@code '+'} (<tt>'&#92;u002b'</tt>).
1081  *
1082  *     <p> If the {@code '#'} flag is given then the output will always begin
1083  *     with {@code '0'} prefix.


1138  * Long apply.  The <a href="#intdFlags">default behavior</a> when no flags are
1139  * given is the same as for Byte, Short, Integer, and Long.
1140  *
1141  * <p> The specification of <a href="#intWidth">width</a> is the same as
1142  * defined for Byte, Short, Integer, and Long.
1143  *
1144  * <p> The precision is not applicable.  If precision is specified then an
1145  * {@link IllegalFormatPrecisionException} will be thrown.
1146  *
1147  * <p><a name="dndec"><b> Float and Double</b></a>
1148  *
1149  * <p> The following conversions may be applied to {@code float}, {@link
1150  * Float}, {@code double} and {@link Double}.
1151  *
1152  * <table cellpadding=5 summary="floatConv">
1153  *
1154  * <tr><td valign="top"> {@code 'e'}
1155  *     <td valign="top"> <tt>'&#92;u0065'</tt>
1156  *     <td> Requires the output to be formatted using <a
1157  *     name="scientific">computerized scientific notation</a>.  The <a
1158  *     href="#L10nAlgorithm">localization algorithm</a> is applied.
1159  *
1160  *     <p> The formatting of the magnitude <i>m</i> depends upon its value.
1161  *
1162  *     <p> If <i>m</i> is NaN or infinite, the literal strings "NaN" or
1163  *     "Infinity", respectively, will be output.  These values are not
1164  *     localized.
1165  *
1166  *     <p> If <i>m</i> is positive-zero or negative-zero, then the exponent
1167  *     will be {@code "+00"}.
1168  *
1169  *     <p> Otherwise, the result is a string that represents the sign and
1170  *     magnitude (absolute value) of the argument.  The formatting of the sign
1171  *     is described in the <a href="#L10nAlgorithm">localization
1172  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1173  *     value.
1174  *
1175  *     <p> Let <i>n</i> be the unique integer such that 10<sup><i>n</i></sup>
1176  *     &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
1177  *     mathematically exact quotient of <i>m</i> and 10<sup><i>n</i></sup> so
1178  *     that 1 &lt;= <i>a</i> &lt; 10. The magnitude is then represented as the
1179  *     integer part of <i>a</i>, as a single decimal digit, followed by the
1180  *     decimal separator followed by decimal digits representing the fractional
1181  *     part of <i>a</i>, followed by the exponent symbol {@code 'e'}
1182  *     (<tt>'&#92;u0065'</tt>), followed by the sign of the exponent, followed
1183  *     by a representation of <i>n</i> as a decimal integer, as produced by the
1184  *     method {@link Long#toString(long, int)}, and zero-padded to include at
1185  *     least two digits.
1186  *
1187  *     <p> The number of digits in the result for the fractional part of
1188  *     <i>m</i> or <i>a</i> is equal to the precision.  If the precision is not
1189  *     specified then the default value is {@code 6}. If the precision is less
1190  *     than the number of digits which would appear after the decimal point in
1191  *     the string returned by {@link Float#toString(float)} or {@link
1192  *     Double#toString(double)} respectively, then the value will be rounded
1193  *     using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1194  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1195  *     For a canonical representation of the value, use {@link
1196  *     Float#toString(float)} or {@link Double#toString(double)} as
1197  *     appropriate.
1198  *
1199  *     <p>If the {@code ','} flag is given, then an {@link
1200  *     FormatFlagsConversionMismatchException} will be thrown.
1201  *
1202  * <tr><td valign="top"> {@code 'E'}
1203  *     <td valign="top"> <tt>'&#92;u0045'</tt>
1204  *     <td> The upper-case variant of {@code 'e'}.  The exponent symbol
1205  *     will be {@code 'E'} (<tt>'&#92;u0045'</tt>).
1206  *
1207  * <tr><td valign="top"> {@code 'g'}
1208  *     <td valign="top"> <tt>'&#92;u0067'</tt>
1209  *     <td> Requires the output to be formatted in general scientific notation
1210  *     as described below. The <a href="#L10nAlgorithm">localization
1211  *     algorithm</a> is applied.
1212  *
1213  *     <p> After rounding for the precision, the formatting of the resulting
1214  *     magnitude <i>m</i> depends on its value.
1215  *
1216  *     <p> If <i>m</i> is greater than or equal to 10<sup>-4</sup> but less
1217  *     than 10<sup>precision</sup> then it is represented in <i><a
1218  *     href="#decimal">decimal format</a></i>.
1219  *
1220  *     <p> If <i>m</i> is less than 10<sup>-4</sup> or greater than or equal to
1221  *     10<sup>precision</sup>, then it is represented in <i><a
1222  *     href="#scientific">computerized scientific notation</a></i>.
1223  *
1224  *     <p> The total number of significant digits in <i>m</i> is equal to the
1225  *     precision.  If the precision is not specified, then the default value is
1226  *     {@code 6}.  If the precision is {@code 0}, then it is taken to be
1227  *     {@code 1}.
1228  *
1229  *     <p> If the {@code '#'} flag is given then an {@link
1230  *     FormatFlagsConversionMismatchException} will be thrown.
1231  *
1232  * <tr><td valign="top"> {@code 'G'}
1233  *     <td valign="top"> <tt>'&#92;u0047'</tt>
1234  *     <td> The upper-case variant of {@code 'g'}.
1235  *
1236  * <tr><td valign="top"> {@code 'f'}
1237  *     <td valign="top"> <tt>'&#92;u0066'</tt>
1238  *     <td> Requires the output to be formatted using <a name="decimal">decimal
1239  *     format</a>.  The <a href="#L10nAlgorithm">localization algorithm</a> is
1240  *     applied.
1241  *
1242  *     <p> The result is a string that represents the sign and magnitude
1243  *     (absolute value) of the argument.  The formatting of the sign is
1244  *     described in the <a href="#L10nAlgorithm">localization
1245  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1246  *     value.
1247  *
1248  *     <p> If <i>m</i> NaN or infinite, the literal strings "NaN" or
1249  *     "Infinity", respectively, will be output.  These values are not
1250  *     localized.
1251  *
1252  *     <p> The magnitude is formatted as the integer part of <i>m</i>, with no
1253  *     leading zeroes, followed by the decimal separator followed by one or
1254  *     more decimal digits representing the fractional part of <i>m</i>.
1255  *
1256  *     <p> The number of digits in the result for the fractional part of
1257  *     <i>m</i> or <i>a</i> is equal to the precision.  If the precision is not
1258  *     specified then the default value is {@code 6}. If the precision is less
1259  *     than the number of digits which would appear after the decimal point in
1260  *     the string returned by {@link Float#toString(float)} or {@link
1261  *     Double#toString(double)} respectively, then the value will be rounded
1262  *     using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1263  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1264  *     For a canonical representation of the value, use {@link


1365  * rounding.  If the precision is not specified, then the default value is
1366  * {@code 6}.  If the precision is {@code 0}, then it is taken to be
1367  * {@code 1}.
1368  *
1369  * <p> If the conversion is {@code 'a'} or {@code 'A'}, then the precision
1370  * is the number of hexadecimal digits after the decimal separator.  If the
1371  * precision is not provided, then all of the digits as returned by {@link
1372  * Double#toHexString(double)} will be output.
1373  *
1374  * <p><a name="dnbdec"><b> BigDecimal </b></a>
1375  *
1376  * <p> The following conversions may be applied {@link java.math.BigDecimal
1377  * BigDecimal}.
1378  *
1379  * <table cellpadding=5 summary="floatConv">
1380  *
1381  * <tr><td valign="top"> {@code 'e'}
1382  *     <td valign="top"> <tt>'&#92;u0065'</tt>
1383  *     <td> Requires the output to be formatted using <a
1384  *     name="bscientific">computerized scientific notation</a>.  The <a
1385  *     href="#L10nAlgorithm">localization algorithm</a> is applied.
1386  *
1387  *     <p> The formatting of the magnitude <i>m</i> depends upon its value.
1388  *
1389  *     <p> If <i>m</i> is positive-zero or negative-zero, then the exponent
1390  *     will be {@code "+00"}.
1391  *
1392  *     <p> Otherwise, the result is a string that represents the sign and
1393  *     magnitude (absolute value) of the argument.  The formatting of the sign
1394  *     is described in the <a href="#L10nAlgorithm">localization
1395  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1396  *     value.
1397  *
1398  *     <p> Let <i>n</i> be the unique integer such that 10<sup><i>n</i></sup>
1399  *     &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
1400  *     mathematically exact quotient of <i>m</i> and 10<sup><i>n</i></sup> so
1401  *     that 1 &lt;= <i>a</i> &lt; 10. The magnitude is then represented as the
1402  *     integer part of <i>a</i>, as a single decimal digit, followed by the
1403  *     decimal separator followed by decimal digits representing the fractional
1404  *     part of <i>a</i>, followed by the exponent symbol {@code 'e'}
1405  *     (<tt>'&#92;u0065'</tt>), followed by the sign of the exponent, followed
1406  *     by a representation of <i>n</i> as a decimal integer, as produced by the
1407  *     method {@link Long#toString(long, int)}, and zero-padded to include at
1408  *     least two digits.
1409  *
1410  *     <p> The number of digits in the result for the fractional part of
1411  *     <i>m</i> or <i>a</i> is equal to the precision.  If the precision is not
1412  *     specified then the default value is {@code 6}.  If the precision is
1413  *     less than the number of digits to the right of the decimal point then
1414  *     the value will be rounded using the
1415  *     {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1416  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1417  *     For a canonical representation of the value, use {@link
1418  *     BigDecimal#toString()}.
1419  *
1420  *     <p> If the {@code ','} flag is given, then an {@link
1421  *     FormatFlagsConversionMismatchException} will be thrown.
1422  *
1423  * <tr><td valign="top"> {@code 'E'}
1424  *     <td valign="top"> <tt>'&#92;u0045'</tt>
1425  *     <td> The upper-case variant of {@code 'e'}.  The exponent symbol
1426  *     will be {@code 'E'} (<tt>'&#92;u0045'</tt>).
1427  *
1428  * <tr><td valign="top"> {@code 'g'}
1429  *     <td valign="top"> <tt>'&#92;u0067'</tt>
1430  *     <td> Requires the output to be formatted in general scientific notation
1431  *     as described below. The <a href="#L10nAlgorithm">localization
1432  *     algorithm</a> is applied.
1433  *
1434  *     <p> After rounding for the precision, the formatting of the resulting
1435  *     magnitude <i>m</i> depends on its value.
1436  *
1437  *     <p> If <i>m</i> is greater than or equal to 10<sup>-4</sup> but less
1438  *     than 10<sup>precision</sup> then it is represented in <i><a
1439  *     href="#bdecimal">decimal format</a></i>.
1440  *
1441  *     <p> If <i>m</i> is less than 10<sup>-4</sup> or greater than or equal to
1442  *     10<sup>precision</sup>, then it is represented in <i><a
1443  *     href="#bscientific">computerized scientific notation</a></i>.
1444  *
1445  *     <p> The total number of significant digits in <i>m</i> is equal to the
1446  *     precision.  If the precision is not specified, then the default value is
1447  *     {@code 6}.  If the precision is {@code 0}, then it is taken to be
1448  *     {@code 1}.
1449  *
1450  *     <p> If the {@code '#'} flag is given then an {@link
1451  *     FormatFlagsConversionMismatchException} will be thrown.
1452  *
1453  * <tr><td valign="top"> {@code 'G'}
1454  *     <td valign="top"> <tt>'&#92;u0047'</tt>
1455  *     <td> The upper-case variant of {@code 'g'}.
1456  *
1457  * <tr><td valign="top"> {@code 'f'}
1458  *     <td valign="top"> <tt>'&#92;u0066'</tt>
1459  *     <td> Requires the output to be formatted using <a name="bdecimal">decimal
1460  *     format</a>.  The <a href="#L10nAlgorithm">localization algorithm</a> is
1461  *     applied.
1462  *
1463  *     <p> The result is a string that represents the sign and magnitude
1464  *     (absolute value) of the argument.  The formatting of the sign is
1465  *     described in the <a href="#L10nAlgorithm">localization
1466  *     algorithm</a>. The formatting of the magnitude <i>m</i> depends upon its
1467  *     value.
1468  *
1469  *     <p> The magnitude is formatted as the integer part of <i>m</i>, with no
1470  *     leading zeroes, followed by the decimal separator followed by one or
1471  *     more decimal digits representing the fractional part of <i>m</i>.
1472  *
1473  *     <p> The number of digits in the result for the fractional part of
1474  *     <i>m</i> or <i>a</i> is equal to the precision. If the precision is not
1475  *     specified then the default value is {@code 6}.  If the precision is
1476  *     less than the number of digits to the right of the decimal point
1477  *     then the value will be rounded using the
1478  *     {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
1479  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
1480  *     For a canonical representation of the value, use {@link
1481  *     BigDecimal#toString()}.
1482  *
1483  * </table>
1484  *
1485  * <p> All <a href="#intFlags">flags</a> defined for Byte, Short, Integer, and


1704  * <tr><td valign="top">{@code 'D'}
1705  *     <td valign="top"> <tt>'&#92;u0044'</tt>
1706  *     <td> Date formatted as {@code "%tm/%td/%ty"}.
1707  *
1708  * <tr><td valign="top">{@code 'F'}
1709  *     <td valign="top"> <tt>'&#92;u0046'</tt>
1710  *     <td> <a href="http://www.w3.org/TR/NOTE-datetime">ISO&nbsp;8601</a>
1711  *     complete date formatted as {@code "%tY-%tm-%td"}.
1712  *
1713  * <tr><td valign="top">{@code 'c'}
1714  *     <td valign="top"> <tt>'&#92;u0063'</tt>
1715  *     <td> Date and time formatted as {@code "%ta %tb %td %tT %tZ %tY"},
1716  *     e.g. {@code "Sun Jul 20 16:17:00 EDT 1969"}.
1717  *
1718  * </table>
1719  *
1720  * <p> The {@code '-'} flag defined for <a href="#dFlags">General
1721  * conversions</a> applies.  If the {@code '#'} flag is given, then a {@link
1722  * FormatFlagsConversionMismatchException} will be thrown.
1723  *
1724  * <p> The width is the minimum number of characters to
1725  * be written to the output.  If the length of the converted value is less than
1726  * the {@code width} then the output will be padded by spaces
1727  * (<tt>'&#92;u0020'</tt>) until the total number of characters equals width.
1728  * The padding is on the left by default.  If the {@code '-'} flag is given
1729  * then the padding will be on the right.  If width is not specified then there
1730  * is no minimum.
1731  *
1732  * <p> The precision is not applicable.  If the precision is specified then an
1733  * {@link IllegalFormatPrecisionException} will be thrown.
1734  *
1735  * <h4><a name="dper">Percent</a></h4>
1736  *
1737  * <p> The conversion does not correspond to any argument.
1738  *
1739  * <table cellpadding=5 summary="DTConv">
1740  *
1741  * <tr><td valign="top">{@code '%'}
1742  *     <td> The result is a literal {@code '%'} (<tt>'&#92;u0025'</tt>)
1743  *
1744  * <p> The width is the minimum number of characters to
1745  * be written to the output including the {@code '%'}.  If the length of the
1746  * converted value is less than the {@code width} then the output will be
1747  * padded by spaces (<tt>'&#92;u0020'</tt>) until the total number of
1748  * characters equals width.  The padding is on the left.  If width is not
1749  * specified then just the {@code '%'} is output.
1750  *
1751  * <p> The {@code '-'} flag defined for <a href="#dFlags">General
1752  * conversions</a> applies.  If any other flags are provided, then a
1753  * {@link FormatFlagsConversionMismatchException} will be thrown.
1754  *
1755  * <p> The precision is not applicable.  If the precision is specified an
1756  * {@link IllegalFormatPrecisionException} will be thrown.
1757  *
1758  * </table>
1759  *
1760  * <h4><a name="dls">Line Separator</a></h4>
1761  *
1762  * <p> The conversion does not correspond to any argument.
1763  *
1764  * <table cellpadding=5 summary="DTConv">


2573                 throw new UnknownFormatConversionException(String.valueOf(c));
2574             }
2575         }
2576     }
2577 
2578     private interface FormatString {
2579         int index();
2580         void print(Object arg, Locale l) throws IOException;
2581         String toString();
2582     }
2583 
2584     private class FixedString implements FormatString {
2585         private String s;
2586         FixedString(String s) { this.s = s; }
2587         public int index() { return -2; }
2588         public void print(Object arg, Locale l)
2589             throws IOException { a.append(s); }
2590         public String toString() { return s; }
2591     }
2592 
2593     /**
2594      * Enum for {@code BigDecimal} formatting.
2595      */
2596     public enum BigDecimalLayoutForm {
2597         /**
2598          * Format the {@code BigDecimal} in computerized scientific notation.
2599          */
2600         SCIENTIFIC,
2601 
2602         /**
2603          * Format the {@code BigDecimal} as a decimal number.
2604          */
2605         DECIMAL_FLOAT
2606     };
2607 
2608     private class FormatSpecifier implements FormatString {
2609         private int index = -1;
2610         private Flags f = Flags.NONE;
2611         private int width;
2612         private int precision;
2613         private boolean dt = false;
2614         private char c;
2615 
2616         private int index(String s) {
2617             if (s != null) {
2618                 try {
2619                     index = Integer.parseInt(s.substring(0, s.length() - 1));
2620                 } catch (NumberFormatException x) {
2621                     assert(false);
2622                 }
2623             } else {
2624                 index = 0;
2625             }
2626             return index;