| | |
| | | * In format strings containing the %<code>n</code>$ |
| | | * form of conversion specifications, each argument |
| | | * in the argument list is used exactly once.</p> |
| | | * |
| | | * <p/> |
| | | * <h4>Escape Sequences</h4> |
| | | * <p> |
| | | * The following table lists escape sequences and |
| | |
| | | * the action. |
| | | * <table> |
| | | * <tr><th align=left>Sequence</th> |
| | | * <th align=left>Name</th> |
| | | * <th align=left>Description</th></tr> |
| | | * <th align=left>Name</th> |
| | | * <th align=left>Description</th></tr> |
| | | * <tr><td>\\</td><td>backlash</td><td>None. |
| | | * </td></tr> |
| | | * <tr><td>\a</td><td>alert</td><td>Attempts to alert |
| | | * the user through audible or visible |
| | | * notification. |
| | | * the user through audible or visible |
| | | * notification. |
| | | * </td></tr> |
| | | * <tr><td>\b</td><td>backspace</td><td>Moves the |
| | | * printing position to one column before |
| | | * the current position, unless the |
| | | * current position is the start of a line. |
| | | * printing position to one column before |
| | | * the current position, unless the |
| | | * current position is the start of a line. |
| | | * </td></tr> |
| | | * <tr><td>\f</td><td>form-feed</td><td>Moves the |
| | | * printing position to the initial |
| | | * printing position of the next logical |
| | | * page. |
| | | * printing position to the initial |
| | | * printing position of the next logical |
| | | * page. |
| | | * </td></tr> |
| | | * <tr><td>\n</td><td>newline</td><td>Moves the |
| | | * printing position to the start of the |
| | | * next line. |
| | | * printing position to the start of the |
| | | * next line. |
| | | * </td></tr> |
| | | * <tr><td>\r</td><td>carriage-return</td><td>Moves |
| | | * the printing position to the start of |
| | | * the current line. |
| | | * the printing position to the start of |
| | | * the current line. |
| | | * </td></tr> |
| | | * <tr><td>\t</td><td>tab</td><td>Moves the printing |
| | | * position to the next implementation- |
| | | * defined horizontal tab position. |
| | | * position to the next implementation- |
| | | * defined horizontal tab position. |
| | | * </td></tr> |
| | | * <tr><td>\v</td><td>vertical-tab</td><td>Moves the |
| | | * printing position to the start of the |
| | | * next implementation-defined vertical |
| | | * tab position. |
| | | * printing position to the start of the |
| | | * next implementation-defined vertical |
| | | * tab position. |
| | | * </td></tr> |
| | | * </table></p> |
| | | * <h4>Conversion Specifications</h4> |
| | |
| | | * be mixed with the %<code>n</code>$ form. The |
| | | * results of mixing numbered and unnumbered argument |
| | | * specifications in a format string are undefined.</p> |
| | | * |
| | | * <p/> |
| | | * <h4>Flag Characters</h4> |
| | | * <p> |
| | | * The flags and their meanings are:</p> |
| | | * <dl> |
| | | * <dt>'<dd> integer portion of the result of a |
| | | * decimal conversion (%i, %d, %f, %g, or %G) will |
| | | * be formatted with thousands' grouping |
| | | * characters. For other conversions the flag |
| | | * is ignored. The non-monetary grouping |
| | | * character is used. |
| | | * decimal conversion (%i, %d, %f, %g, or %G) will |
| | | * be formatted with thousands' grouping |
| | | * characters. For other conversions the flag |
| | | * is ignored. The non-monetary grouping |
| | | * character is used. |
| | | * <dt>-<dd> result of the conversion is left-justified |
| | | * within the field. (It will be right-justified |
| | | * if this flag is not specified).</td></tr> |
| | | * within the field. (It will be right-justified |
| | | * if this flag is not specified).</td></tr> |
| | | * <dt>+<dd> result of a signed conversion always |
| | | * begins with a sign (+ or -). (It will begin |
| | | * with a sign only when a negative value is |
| | | * converted if this flag is not specified.) |
| | | * begins with a sign (+ or -). (It will begin |
| | | * with a sign only when a negative value is |
| | | * converted if this flag is not specified.) |
| | | * <dt><space><dd> If the first character of a |
| | | * signed conversion is not a sign, a space |
| | | * character will be placed before the result. |
| | | * This means that if the space character and + |
| | | * flags both appear, the space flag will be |
| | | * ignored. |
| | | * signed conversion is not a sign, a space |
| | | * character will be placed before the result. |
| | | * This means that if the space character and + |
| | | * flags both appear, the space flag will be |
| | | * ignored. |
| | | * <dt>#<dd> value is to be converted to an alternative |
| | | * form. For c, d, i, and s conversions, the flag |
| | | * has no effect. For o conversion, it increases |
| | | * the precision to force the first digit of the |
| | | * result to be a zero. For x or X conversion, a |
| | | * non-zero result has 0x or 0X prefixed to it, |
| | | * respectively. For e, E, f, g, and G |
| | | * conversions, the result always contains a radix |
| | | * character, even if no digits follow the radix |
| | | * character (normally, a decimal point appears in |
| | | * the result of these conversions only if a digit |
| | | * follows it). For g and G conversions, trailing |
| | | * zeros will not be removed from the result as |
| | | * they normally are. |
| | | * form. For c, d, i, and s conversions, the flag |
| | | * has no effect. For o conversion, it increases |
| | | * the precision to force the first digit of the |
| | | * result to be a zero. For x or X conversion, a |
| | | * non-zero result has 0x or 0X prefixed to it, |
| | | * respectively. For e, E, f, g, and G |
| | | * conversions, the result always contains a radix |
| | | * character, even if no digits follow the radix |
| | | * character (normally, a decimal point appears in |
| | | * the result of these conversions only if a digit |
| | | * follows it). For g and G conversions, trailing |
| | | * zeros will not be removed from the result as |
| | | * they normally are. |
| | | * <dt>0<dd> d, i, o, x, X, e, E, f, g, and G |
| | | * conversions, leading zeros (following any |
| | | * indication of sign or base) are used to pad to |
| | | * the field width; no space padding is |
| | | * performed. If the 0 and - flags both appear, |
| | | * the 0 flag is ignored. For d, i, o, x, and X |
| | | * conversions, if a precision is specified, the |
| | | * 0 flag will be ignored. For c conversions, |
| | | * the flag is ignored. |
| | | * conversions, leading zeros (following any |
| | | * indication of sign or base) are used to pad to |
| | | * the field width; no space padding is |
| | | * performed. If the 0 and - flags both appear, |
| | | * the 0 flag is ignored. For d, i, o, x, and X |
| | | * conversions, if a precision is specified, the |
| | | * 0 flag will be ignored. For c conversions, |
| | | * the flag is ignored. |
| | | * </dl> |
| | | * |
| | | * <p/> |
| | | * <h4>Conversion Characters</h4> |
| | | * <p> |
| | | * Each conversion character results in fetching zero |
| | |
| | | * Usually, an unchecked exception will be thrown. |
| | | * If the format is exhausted while arguments remain, |
| | | * the excess arguments are ignored.</p> |
| | | * |
| | | * <p/> |
| | | * <p> |
| | | * The conversion characters and their meanings are: |
| | | * </p> |
| | | * <dl> |
| | | * <dt>d,i<dd>The int argument is converted to a |
| | | * signed decimal in the style [-]dddd. The |
| | | * precision specifies the minimum number of |
| | | * digits to appear; if the value being |
| | | * converted can be represented in fewer |
| | | * digits, it will be expanded with leading |
| | | * zeros. The default precision is 1. The |
| | | * result of converting 0 with an explicit |
| | | * precision of 0 is no characters. |
| | | * signed decimal in the style [-]dddd. The |
| | | * precision specifies the minimum number of |
| | | * digits to appear; if the value being |
| | | * converted can be represented in fewer |
| | | * digits, it will be expanded with leading |
| | | * zeros. The default precision is 1. The |
| | | * result of converting 0 with an explicit |
| | | * precision of 0 is no characters. |
| | | * <dt>o<dd> The int argument is converted to unsigned |
| | | * octal format in the style ddddd. The |
| | | * precision specifies the minimum number of |
| | | * digits to appear; if the value being |
| | | * converted can be represented in fewer |
| | | * digits, it will be expanded with leading |
| | | * zeros. The default precision is 1. The |
| | | * result of converting 0 with an explicit |
| | | * precision of 0 is no characters. |
| | | * octal format in the style ddddd. The |
| | | * precision specifies the minimum number of |
| | | * digits to appear; if the value being |
| | | * converted can be represented in fewer |
| | | * digits, it will be expanded with leading |
| | | * zeros. The default precision is 1. The |
| | | * result of converting 0 with an explicit |
| | | * precision of 0 is no characters. |
| | | * <dt>x<dd> The int argument is converted to unsigned |
| | | * hexadecimal format in the style dddd; the |
| | | * letters abcdef are used. The precision |
| | | * specifies the minimum numberof digits to |
| | | * appear; if the value being converted can be |
| | | * represented in fewer digits, it will be |
| | | * expanded with leading zeros. The default |
| | | * precision is 1. The result of converting 0 |
| | | * with an explicit precision of 0 is no |
| | | * characters. |
| | | * hexadecimal format in the style dddd; the |
| | | * letters abcdef are used. The precision |
| | | * specifies the minimum numberof digits to |
| | | * appear; if the value being converted can be |
| | | * represented in fewer digits, it will be |
| | | * expanded with leading zeros. The default |
| | | * precision is 1. The result of converting 0 |
| | | * with an explicit precision of 0 is no |
| | | * characters. |
| | | * <dt>X<dd> Behaves the same as the x conversion |
| | | * character except that letters ABCDEF are |
| | | * used instead of abcdef. |
| | | * character except that letters ABCDEF are |
| | | * used instead of abcdef. |
| | | * <dt>f<dd> The floating point number argument is |
| | | * written in decimal notation in the style |
| | | * [-]ddd.ddd, where the number of digits after |
| | | * the radix character (shown here as a decimal |
| | | * point) is equal to the precision |
| | | * specification. A Locale is used to determine |
| | | * the radix character to use in this format. |
| | | * If the precision is omitted from the |
| | | * argument, six digits are written after the |
| | | * radix character; if the precision is |
| | | * explicitly 0 and the # flag is not specified, |
| | | * no radix character appears. If a radix |
| | | * character appears, at least 1 digit appears |
| | | * before it. The value is rounded to the |
| | | * appropriate number of digits. |
| | | * written in decimal notation in the style |
| | | * [-]ddd.ddd, where the number of digits after |
| | | * the radix character (shown here as a decimal |
| | | * point) is equal to the precision |
| | | * specification. A Locale is used to determine |
| | | * the radix character to use in this format. |
| | | * If the precision is omitted from the |
| | | * argument, six digits are written after the |
| | | * radix character; if the precision is |
| | | * explicitly 0 and the # flag is not specified, |
| | | * no radix character appears. If a radix |
| | | * character appears, at least 1 digit appears |
| | | * before it. The value is rounded to the |
| | | * appropriate number of digits. |
| | | * <dt>e,E<dd>The floating point number argument is |
| | | * written in the style [-]d.ddde{+-}dd |
| | | * (the symbols {+-} indicate either a plus or |
| | | * minus sign), where there is one digit before |
| | | * the radix character (shown here as a decimal |
| | | * point) and the number of digits after it is |
| | | * equal to the precision. A Locale is used to |
| | | * determine the radix character to use in this |
| | | * format. When the precision is missing, six |
| | | * digits are written after the radix character; |
| | | * if the precision is 0 and the # flag is not |
| | | * specified, no radix character appears. The |
| | | * E conversion will produce a number with E |
| | | * instead of e introducing the exponent. The |
| | | * exponent always contains at least two digits. |
| | | * However, if the value to be written requires |
| | | * an exponent greater than two digits, |
| | | * additional exponent digits are written as |
| | | * necessary. The value is rounded to the |
| | | * appropriate number of digits. |
| | | * written in the style [-]d.ddde{+-}dd |
| | | * (the symbols {+-} indicate either a plus or |
| | | * minus sign), where there is one digit before |
| | | * the radix character (shown here as a decimal |
| | | * point) and the number of digits after it is |
| | | * equal to the precision. A Locale is used to |
| | | * determine the radix character to use in this |
| | | * format. When the precision is missing, six |
| | | * digits are written after the radix character; |
| | | * if the precision is 0 and the # flag is not |
| | | * specified, no radix character appears. The |
| | | * E conversion will produce a number with E |
| | | * instead of e introducing the exponent. The |
| | | * exponent always contains at least two digits. |
| | | * However, if the value to be written requires |
| | | * an exponent greater than two digits, |
| | | * additional exponent digits are written as |
| | | * necessary. The value is rounded to the |
| | | * appropriate number of digits. |
| | | * <dt>g,G<dd>The floating point number argument is |
| | | * written in style f or e (or in sytle E in the |
| | | * case of a G conversion character), with the |
| | | * precision specifying the number of |
| | | * significant digits. If the precision is |
| | | * zero, it is taken as one. The style used |
| | | * depends on the value converted: style e |
| | | * (or E) will be used only if the exponent |
| | | * resulting from the conversion is less than |
| | | * -4 or greater than or equal to the precision. |
| | | * Trailing zeros are removed from the result. |
| | | * A radix character appears only if it is |
| | | * followed by a digit. |
| | | * written in style f or e (or in sytle E in the |
| | | * case of a G conversion character), with the |
| | | * precision specifying the number of |
| | | * significant digits. If the precision is |
| | | * zero, it is taken as one. The style used |
| | | * depends on the value converted: style e |
| | | * (or E) will be used only if the exponent |
| | | * resulting from the conversion is less than |
| | | * -4 or greater than or equal to the precision. |
| | | * Trailing zeros are removed from the result. |
| | | * A radix character appears only if it is |
| | | * followed by a digit. |
| | | * <dt>c,C<dd>The integer argument is converted to a |
| | | * char and the result is written. |
| | | * |
| | | * char and the result is written. |
| | | * <p/> |
| | | * <dt>s,S<dd>The argument is taken to be a string and |
| | | * bytes from the string are written until the |
| | | * end of the string or the number of bytes |
| | | * indicated by the precision specification of |
| | | * the argument is reached. If the precision |
| | | * is omitted from the argument, it is taken to |
| | | * be infinite, so all characters up to the end |
| | | * of the string are written. |
| | | * bytes from the string are written until the |
| | | * end of the string or the number of bytes |
| | | * indicated by the precision specification of |
| | | * the argument is reached. If the precision |
| | | * is omitted from the argument, it is taken to |
| | | * be infinite, so all characters up to the end |
| | | * of the string are written. |
| | | * <dt>%<dd>Write a % character; no argument is |
| | | * converted. |
| | | * converted. |
| | | * </dl> |
| | | * <p> |
| | | * If a conversion specification does not match one of |
| | |
| | | * |
| | | * @author Allan Jacobs |
| | | * @version 1 |
| | | * Release 1: Initial release. |
| | | * Release 2: Asterisk field widths and precisions |
| | | * %n$ and *m$ |
| | | * Bug fixes |
| | | * g format fix (2 digits in e form corrupt) |
| | | * rounding in f format implemented |
| | | * round up when digit not printed is 5 |
| | | * formatting of -0.0f |
| | | * round up/down when last digits are 50000... |
| | | * Release 1: Initial release. |
| | | * Release 2: Asterisk field widths and precisions |
| | | * %n$ and *m$ |
| | | * Bug fixes |
| | | * g format fix (2 digits in e form corrupt) |
| | | * rounding in f format implemented |
| | | * round up when digit not printed is 5 |
| | | * formatting of -0.0f |
| | | * round up/down when last digits are 50000... |
| | | */ |
| | | public final class PrintfFormat |
| | | { |
| | | /** Vector of control strings and format literals. */ |
| | | /** |
| | | * Vector of control strings and format literals. |
| | | */ |
| | | private Vector vFmt = new Vector(); |
| | | |
| | | /** Character position. Used by the constructor. */ |
| | | /** |
| | | * Character position. Used by the constructor. |
| | | */ |
| | | private int cPos = 0; |
| | | |
| | | /** Character position. Used by the constructor. */ |
| | | /** |
| | | * Character position. Used by the constructor. |
| | | */ |
| | | private DecimalFormatSymbols dfs = null; |
| | | |
| | | /** |
| | |
| | | * unpaired percent signs. A pair of successive |
| | | * percent signs designates a single percent sign in |
| | | * the format. |
| | | * @param fmtArg Control string. |
| | | * @exception IllegalArgumentException if the control |
| | | * string is null, zero length, or otherwise |
| | | * malformed. |
| | | * |
| | | * @param fmtArg Control string. |
| | | * @throws IllegalArgumentException if the control |
| | | * string is null, zero length, or otherwise |
| | | * malformed. |
| | | */ |
| | | public PrintfFormat(String fmtArg) throws IllegalArgumentException |
| | | { |
| | |
| | | * unpaired percent signs. A pair of successive |
| | | * percent signs designates a single percent sign in |
| | | * the format. |
| | | * @param fmtArg Control string. |
| | | * @exception IllegalArgumentException if the control |
| | | * string is null, zero length, or otherwise |
| | | * malformed. |
| | | * |
| | | * @param fmtArg Control string. |
| | | * @throws IllegalArgumentException if the control |
| | | * string is null, zero length, or otherwise |
| | | * malformed. |
| | | */ |
| | | public PrintfFormat(Locale locale, String fmtArg) throws IllegalArgumentException |
| | | { |
| | | dfs = new DecimalFormatSymbols(locale); |
| | | |
| | | int ePos = 0; |
| | | int ePos = 0; |
| | | ConversionSpecification sFmt = null; |
| | | String unCS = this.nonControl(fmtArg, 0); |
| | | String unCS = this.nonControl(fmtArg, 0); |
| | | |
| | | if (unCS != null) |
| | | { |
| | |
| | | * of the String <code>s</code>, the next unpaired |
| | | * percent sign, or at the end of the String if the |
| | | * last character is a percent sign. |
| | | * @param s Control string. |
| | | * |
| | | * @param s Control string. |
| | | * @param start Position in the string |
| | | * <code>s</code> to begin looking for the start |
| | | * of a control string. |
| | | * <code>s</code> to begin looking for the start |
| | | * of a control string. |
| | | * @return the substring from the start position |
| | | * to the beginning of the control string. |
| | | * to the beginning of the control string. |
| | | */ |
| | | private String nonControl(String s, int start) |
| | | { |
| | |
| | | * Integer, Long, Float, Double, and Character |
| | | * arguments are treated as wrappers for primitive |
| | | * types. |
| | | * |
| | | * @param o The array of objects to format. |
| | | * @return The formatted String. |
| | | * @return The formatted String. |
| | | */ |
| | | public String sprintf(Object[] o) |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | int i = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | int i = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | |
| | | /** |
| | | * Format nothing. Just use the control string. |
| | | * @return the formatted String. |
| | | * |
| | | * @return the formatted String. |
| | | */ |
| | | public String sprintf() |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | |
| | | /** |
| | | * Format an int. |
| | | * |
| | | * @param x The int to format. |
| | | * @return The formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, G, s, |
| | | * or S. |
| | | * @return The formatted String. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, G, s, |
| | | * or S. |
| | | */ |
| | | public String sprintf(int x) throws IllegalArgumentException |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | |
| | | /** |
| | | * Format an long. |
| | | * |
| | | * @param x The long to format. |
| | | * @return The formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, G, s, |
| | | * or S. |
| | | * @return The formatted String. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, G, s, |
| | | * or S. |
| | | */ |
| | | public String sprintf(long x) throws IllegalArgumentException |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | |
| | | /** |
| | | * Format a double. |
| | | * |
| | | * @param x The double to format. |
| | | * @return The formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is c, C, s, S, |
| | | * d, d, x, X, or o. |
| | | * @return The formatted String. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is c, C, s, S, |
| | | * d, d, x, X, or o. |
| | | */ |
| | | public String sprintf(double x) throws IllegalArgumentException |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | |
| | | /** |
| | | * Format a String. |
| | | * |
| | | * @param x The String to format. |
| | | * @return The formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is neither s nor S. |
| | | * @return The formatted String. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is neither s nor S. |
| | | */ |
| | | public String sprintf(String x) throws IllegalArgumentException |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | * Strings using an internal formatting method for |
| | | * Strings. Otherwise use the default formatter |
| | | * (use toString). |
| | | * |
| | | * @param x the Object to format. |
| | | * @return the formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is inappropriate for |
| | | * formatting an unwrapped value. |
| | | * @return the formatted String. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is inappropriate for |
| | | * formatting an unwrapped value. |
| | | */ |
| | | public String sprintf(Object x) throws IllegalArgumentException |
| | | { |
| | | Enumeration e = vFmt.elements(); |
| | | Enumeration e = vFmt.elements(); |
| | | ConversionSpecification cs = null; |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | char c = 0; |
| | | StringBuffer sb = new StringBuffer(); |
| | | |
| | | while (e.hasMoreElements()) |
| | | { |
| | | cs = (ConversionSpecification) e.nextElement(); |
| | | c = cs.getConversionCharacter(); |
| | | c = cs.getConversionCharacter(); |
| | | |
| | | if (c == '\0') |
| | | { |
| | |
| | | } |
| | | |
| | | /** |
| | | * <p> |
| | | * <p/> |
| | | * ConversionSpecification allows the formatting of |
| | | * a single primitive or object embedded within a |
| | | * string. The formatting is controlled by a |
| | | * format string. Only one Java primitive or |
| | | * object can be formatted at a time. |
| | | * <p> |
| | | * <p/> |
| | | * A format string is a Java string that contains |
| | | * a control string. The control string starts at |
| | | * the first percent sign (%) in the string, |
| | | * provided that this percent sign |
| | | * <ol> |
| | | * <li>is not escaped protected by a matching % or |
| | | * is not an escape % character, |
| | | * is not an escape % character, |
| | | * <li>is not at the end of the format string, and |
| | | * <li>precedes a sequence of characters that parses |
| | | * as a valid control string. |
| | | * as a valid control string. |
| | | * </ol> |
| | | * <p> |
| | | * <p/> |
| | | * A control string takes the form: |
| | | * <pre> % ['-+ #0]* [0..9]* { . [0..9]* }+ |
| | | * { [hlL] }+ [idfgGoxXeEcs] |
| | | * </pre> |
| | | * <p> |
| | | * <p/> |
| | | * The behavior is like printf. One (hopefully the |
| | | * only) exception is that the minimum number of |
| | | * exponent digits is 3 instead of 2 for e and E |
| | |
| | | */ |
| | | private class ConversionSpecification |
| | | { |
| | | /** Default precision. */ |
| | | /** |
| | | * Default precision. |
| | | */ |
| | | private final static int defaultDigits = 6; |
| | | |
| | | /** |
| | |
| | | * d, i, o, u, x, or X conversions. The number of |
| | | * digits to appear after the radix character for |
| | | * the e, E, and f conversions. The maximum number |
| | | * of significant digits for the g and G |
| | | * of significant digits for the g and G |
| | | * conversions. The maximum number of bytes to be |
| | | * printed from a string in s and S conversions. |
| | | */ |
| | |
| | | |
| | | /* |
| | | */ |
| | | private boolean positionalSpecification = false; |
| | | private int argumentPosition = 0; |
| | | private boolean positionalFieldWidth = false; |
| | | private int argumentPositionForFieldWidth = 0; |
| | | private boolean positionalPrecision = false; |
| | | private int argumentPositionForPrecision = 0; |
| | | private boolean positionalSpecification = false; |
| | | private int argumentPosition = 0; |
| | | private boolean positionalFieldWidth = false; |
| | | private int argumentPositionForFieldWidth = 0; |
| | | private boolean positionalPrecision = false; |
| | | private int argumentPositionForPrecision = 0; |
| | | |
| | | /** |
| | | * Flag specifying that a following d, i, o, u, x, |
| | |
| | | */ |
| | | private boolean optionalL = false; |
| | | |
| | | /** Control string type. */ |
| | | /** |
| | | * Control string type. |
| | | */ |
| | | private char conversionCharacter = '\0'; |
| | | |
| | | /** |
| | |
| | | */ |
| | | private int pos = 0; |
| | | |
| | | /** Literal or control format string. */ |
| | | /** |
| | | * Literal or control format string. |
| | | */ |
| | | private String fmt; |
| | | |
| | | /** |
| | |
| | | * The argument must begin with a % and end |
| | | * with the conversion character for the |
| | | * conversion specification. |
| | | * @param fmtArg String specifying the |
| | | * conversion specification. |
| | | * @exception IllegalArgumentException if the |
| | | * input string is null, zero length, or |
| | | * otherwise malformed. |
| | | * |
| | | * @param fmtArg String specifying the |
| | | * conversion specification. |
| | | * @throws IllegalArgumentException if the |
| | | * input string is null, zero length, or |
| | | * otherwise malformed. |
| | | */ |
| | | ConversionSpecification(String fmtArg) throws IllegalArgumentException |
| | | { |
| | |
| | | if (precisionSet && leadingZeros) |
| | | { |
| | | if ((conversionCharacter == 'd') || (conversionCharacter == 'i') || (conversionCharacter == 'o') |
| | | || (conversionCharacter == 'x')) |
| | | || (conversionCharacter == 'x')) |
| | | { |
| | | leadingZeros = false; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Set the String for this instance. |
| | | * |
| | | * @param s the String to store. |
| | | */ |
| | | void setLiteral(String s) |
| | |
| | | String getLiteral() |
| | | { |
| | | StringBuffer sb = new StringBuffer(); |
| | | int i = 0; |
| | | int i = 0; |
| | | |
| | | while (i < fmt.length()) |
| | | { |
| | |
| | | |
| | | switch (c) |
| | | { |
| | | case 'a' : |
| | | case 'a': |
| | | sb.append((char) 0x07); |
| | | |
| | | break; |
| | | |
| | | case 'b' : |
| | | case 'b': |
| | | sb.append('\b'); |
| | | |
| | | break; |
| | | |
| | | case 'f' : |
| | | case 'f': |
| | | sb.append('\f'); |
| | | |
| | | break; |
| | | |
| | | case 'n' : |
| | | case 'n': |
| | | sb.append(System.getProperty("line.separator")); |
| | | |
| | | break; |
| | | |
| | | case 'r' : |
| | | case 'r': |
| | | sb.append('\r'); |
| | | |
| | | break; |
| | | |
| | | case 't' : |
| | | case 't': |
| | | sb.append('\t'); |
| | | |
| | | break; |
| | | |
| | | case 'v' : |
| | | case 'v': |
| | | sb.append((char) 0x0b); |
| | | |
| | | break; |
| | | |
| | | case '\\' : |
| | | case '\\': |
| | | sb.append('\\'); |
| | | |
| | | break; |
| | |
| | | * Check whether the specifier has a variable |
| | | * field width that is going to be set by an |
| | | * argument. |
| | | * |
| | | * @return <code>true</code> if the conversion |
| | | * uses an * field width; otherwise |
| | | * <code>false</code>. |
| | | * uses an * field width; otherwise |
| | | * <code>false</code>. |
| | | */ |
| | | boolean isVariableFieldWidth() |
| | | { |
| | |
| | | * Set the field width with an argument. A |
| | | * negative field width is taken as a - flag |
| | | * followed by a positive field width. |
| | | * |
| | | * @param fw the field width. |
| | | */ |
| | | void setFieldWidthWithArg(int fw) |
| | |
| | | } |
| | | |
| | | fieldWidthSet = true; |
| | | fieldWidth = Math.abs(fw); |
| | | fieldWidth = Math.abs(fw); |
| | | } |
| | | |
| | | /** |
| | | * Check whether the specifier has a variable |
| | | * precision that is going to be set by an |
| | | * argument. |
| | | * |
| | | * @return <code>true</code> if the conversion |
| | | * uses an * precision; otherwise |
| | | * <code>false</code>. |
| | | * uses an * precision; otherwise |
| | | * <code>false</code>. |
| | | */ |
| | | boolean isVariablePrecision() |
| | | { |
| | |
| | | /** |
| | | * Set the precision with an argument. A |
| | | * negative precision will be changed to zero. |
| | | * |
| | | * @param pr the precision. |
| | | */ |
| | | void setPrecisionWithArg(int pr) |
| | | { |
| | | precisionSet = true; |
| | | precision = Math.max(pr, 0); |
| | | precision = Math.max(pr, 0); |
| | | } |
| | | |
| | | /** |
| | | * Format an int argument using this conversion |
| | | * specification. |
| | | * specification. |
| | | * |
| | | * @param s the int to format. |
| | | * @return the formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, or G. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, or G. |
| | | */ |
| | | String internalsprintf(int s) throws IllegalArgumentException |
| | | { |
| | |
| | | |
| | | switch (conversionCharacter) |
| | | { |
| | | case 'd' : |
| | | case 'i' : |
| | | case 'd': |
| | | case 'i': |
| | | if (optionalh) |
| | | { |
| | | s2 = printDFormat((short) s); |
| | |
| | | |
| | | break; |
| | | |
| | | case 'x' : |
| | | case 'X' : |
| | | case 'x': |
| | | case 'X': |
| | | if (optionalh) |
| | | { |
| | | s2 = printXFormat((short) s); |
| | |
| | | |
| | | break; |
| | | |
| | | case 'o' : |
| | | case 'o': |
| | | if (optionalh) |
| | | { |
| | | s2 = printOFormat((short) s); |
| | |
| | | |
| | | break; |
| | | |
| | | case 'c' : |
| | | case 'C' : |
| | | case 'c': |
| | | case 'C': |
| | | s2 = printCFormat((char) s); |
| | | |
| | | break; |
| | | |
| | | default : |
| | | default: |
| | | throw new IllegalArgumentException("Cannot format a int with a format using a " + conversionCharacter |
| | | + " conversion character."); |
| | | + " conversion character."); |
| | | } |
| | | |
| | | return s2; |
| | |
| | | /** |
| | | * Format a long argument using this conversion |
| | | * specification. |
| | | * |
| | | * @param s the long to format. |
| | | * @return the formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, or G. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is f, e, E, g, or G. |
| | | */ |
| | | String internalsprintf(long s) throws IllegalArgumentException |
| | | { |
| | |
| | | |
| | | switch (conversionCharacter) |
| | | { |
| | | case 'd' : |
| | | case 'i' : |
| | | case 'd': |
| | | case 'i': |
| | | if (optionalh) |
| | | { |
| | | s2 = printDFormat((short) s); |
| | |
| | | |
| | | break; |
| | | |
| | | case 'x' : |
| | | case 'X' : |
| | | case 'x': |
| | | case 'X': |
| | | if (optionalh) |
| | | { |
| | | s2 = printXFormat((short) s); |
| | |
| | | |
| | | break; |
| | | |
| | | case 'o' : |
| | | case 'o': |
| | | if (optionalh) |
| | | { |
| | | s2 = printOFormat((short) s); |
| | |
| | | |
| | | break; |
| | | |
| | | case 'c' : |
| | | case 'C' : |
| | | case 'c': |
| | | case 'C': |
| | | s2 = printCFormat((char) s); |
| | | |
| | | break; |
| | | |
| | | default : |
| | | default: |
| | | throw new IllegalArgumentException("Cannot format a long with a format using a " + conversionCharacter |
| | | + " conversion character."); |
| | | + " conversion character."); |
| | | } |
| | | |
| | | return s2; |
| | |
| | | /** |
| | | * Format a double argument using this conversion |
| | | * specification. |
| | | * |
| | | * @param s the double to format. |
| | | * @return the formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is c, C, s, S, i, d, |
| | | * x, X, or o. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is c, C, s, S, i, d, |
| | | * x, X, or o. |
| | | */ |
| | | String internalsprintf(double s) throws IllegalArgumentException |
| | | { |
| | |
| | | |
| | | switch (conversionCharacter) |
| | | { |
| | | case 'f' : |
| | | case 'f': |
| | | s2 = printFFormat(s); |
| | | |
| | | break; |
| | | |
| | | case 'E' : |
| | | case 'e' : |
| | | case 'E': |
| | | case 'e': |
| | | s2 = printEFormat(s); |
| | | |
| | | break; |
| | | |
| | | case 'G' : |
| | | case 'g' : |
| | | case 'G': |
| | | case 'g': |
| | | s2 = printGFormat(s); |
| | | |
| | | break; |
| | | |
| | | default : |
| | | default: |
| | | throw new IllegalArgumentException("Cannot " + "format a double with a format using a " + conversionCharacter |
| | | + " conversion character."); |
| | | + " conversion character."); |
| | | } |
| | | |
| | | return s2; |
| | |
| | | /** |
| | | * Format a String argument using this conversion |
| | | * specification. |
| | | * |
| | | * @param s the String to format. |
| | | * @return the formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is neither s nor S. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is neither s nor S. |
| | | */ |
| | | String internalsprintf(String s) throws IllegalArgumentException |
| | | { |
| | |
| | | } else |
| | | { |
| | | throw new IllegalArgumentException("Cannot " + "format a String with a format using a " + conversionCharacter |
| | | + " conversion character."); |
| | | + " conversion character."); |
| | | } |
| | | |
| | | return s2; |
| | |
| | | /** |
| | | * Format an Object argument using this conversion |
| | | * specification. |
| | | * |
| | | * @param s the Object to format. |
| | | * @return the formatted String. |
| | | * @exception IllegalArgumentException if the |
| | | * conversion character is neither s nor S. |
| | | * @throws IllegalArgumentException if the |
| | | * conversion character is neither s nor S. |
| | | */ |
| | | String internalsprintf(Object s) |
| | | { |
| | |
| | | } else |
| | | { |
| | | throw new IllegalArgumentException("Cannot format a String with a format using" + " a " + conversionCharacter |
| | | + " conversion character."); |
| | | + " conversion character."); |
| | | } |
| | | |
| | | return s2; |
| | |
| | | * is ignored. The '0' flag character implies that |
| | | * padding to the field width will be done with |
| | | * zeros instead of blanks. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the number of digits |
| | | * to appear after the radix character. Padding is |
| | | * with trailing 0s. |
| | |
| | | private char[] fFormatDigits(double x) |
| | | { |
| | | // int defaultDigits=6; |
| | | String sx, sxOut; |
| | | int i, j, k; |
| | | int n1In, n2In; |
| | | int expon = 0; |
| | | String sx, sxOut; |
| | | int i, j, k; |
| | | int n1In, n2In; |
| | | int expon = 0; |
| | | boolean minusSign = false; |
| | | |
| | | if (x > 0.0) |
| | |
| | | sx = Double.toString(x); |
| | | } else if (x < 0.0) |
| | | { |
| | | sx = Double.toString(-x); |
| | | sx = Double.toString(-x); |
| | | minusSign = true; |
| | | } else |
| | | { |
| | |
| | | if (sx.charAt(0) == '-') |
| | | { |
| | | minusSign = true; |
| | | sx = sx.substring(1); |
| | | sx = sx.substring(1); |
| | | } |
| | | } |
| | | |
| | |
| | | ca4[0] = '1'; |
| | | } |
| | | |
| | | if (alternateForm ||!precisionSet || (precision != 0)) |
| | | if (alternateForm || !precisionSet || (precision != 0)) |
| | | { |
| | | ca4[1] = '.'; |
| | | |
| | |
| | | { |
| | | if (!carry) |
| | | { |
| | | if (alternateForm ||!precisionSet || (precision != 0)) |
| | | if (alternateForm || !precisionSet || (precision != 0)) |
| | | { |
| | | ca4 = new char[n1In + expon + p + 1]; |
| | | } else |
| | |
| | | j = 0; |
| | | } else |
| | | { |
| | | if (alternateForm ||!precisionSet || (precision != 0)) |
| | | if (alternateForm || !precisionSet || (precision != 0)) |
| | | { |
| | | ca4 = new char[n1In + expon + p + 2]; |
| | | } else |
| | |
| | | } |
| | | |
| | | ca4[0] = '1'; |
| | | j = 1; |
| | | j = 1; |
| | | } |
| | | |
| | | for (i = 0; i < Math.min(n1In + expon, ca3.length); i++, j++) |
| | |
| | | ca4[j] = '0'; |
| | | } |
| | | |
| | | if (alternateForm ||!precisionSet || (precision != 0)) |
| | | if (alternateForm || !precisionSet || (precision != 0)) |
| | | { |
| | | ca4[j] = '.'; |
| | | j++; |
| | |
| | | |
| | | if (thousands && (nThousands > 0)) |
| | | { |
| | | ca6 = new char[ca5.length + nThousands + lead]; |
| | | ca6 = new char[ca5.length + nThousands + lead]; |
| | | ca6[0] = ca5[0]; |
| | | |
| | | for (i = lead, k = lead; i < dp; i++) |
| | |
| | | if ((i > 0) && (dp - i) % 3 == 0) |
| | | { |
| | | // ca6[k]=','; |
| | | ca6[k] = dfs.getGroupingSeparator(); |
| | | ca6[k] = dfs.getGroupingSeparator(); |
| | | ca6[k + 1] = ca5[i]; |
| | | k += 2; |
| | | k += 2; |
| | | } else |
| | | { |
| | | ca6[k] = ca5[i]; |
| | |
| | | * the input double value is an infinity, |
| | | * not-a-number, or a finite double and formats |
| | | * each type of input appropriately. |
| | | * |
| | | * @param x the double value to be formatted. |
| | | * @return the converted double value. |
| | | */ |
| | | private String fFormatString(double x) |
| | | { |
| | | boolean noDigits = false; |
| | | char[] ca6, ca7; |
| | | char[] ca6, ca7; |
| | | |
| | | if (Double.isInfinite(x)) |
| | | { |
| | |
| | | * ignored. The '0' flag character implies that |
| | | * padding to the field width will be done with |
| | | * zeros instead of blanks. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear after the radix character. |
| | | * Padding is with trailing 0s. |
| | | * |
| | | * <p/> |
| | | * The behavior is like printf. One (hopefully the |
| | | * only) exception is that the minimum number of |
| | | * exponent digits is 3 instead of 2 for e and E |
| | |
| | | char[] ca1, ca2, ca3; |
| | | |
| | | // int defaultDigits=6; |
| | | String sx, sxOut; |
| | | int i, j, k, p; |
| | | int n1In, n2In; |
| | | int expon = 0; |
| | | int ePos, rPos, eSize; |
| | | String sx, sxOut; |
| | | int i, j, k, p; |
| | | int n1In, n2In; |
| | | int expon = 0; |
| | | int ePos, rPos, eSize; |
| | | boolean minusSign = false; |
| | | |
| | | if (x > 0.0) |
| | |
| | | sx = Double.toString(x); |
| | | } else if (x < 0.0) |
| | | { |
| | | sx = Double.toString(-x); |
| | | sx = Double.toString(-x); |
| | | minusSign = true; |
| | | } else |
| | | { |
| | |
| | | if (sx.charAt(0) == '-') |
| | | { |
| | | minusSign = true; |
| | | sx = sx.substring(1); |
| | | sx = sx.substring(1); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | boolean carry = false; |
| | | int i0 = 0; |
| | | int i0 = 0; |
| | | |
| | | if (ca1[0] != '0') |
| | | { |
| | |
| | | |
| | | if (carry) |
| | | { |
| | | ca2 = new char[i0 + p + 1]; |
| | | ca2 = new char[i0 + p + 1]; |
| | | ca2[i0] = '1'; |
| | | |
| | | for (j = 0; j < i0; j++) |
| | |
| | | } |
| | | } |
| | | |
| | | if ((Math.abs(expon) < 100) &&!optionalL) |
| | | if ((Math.abs(expon) < 100) && !optionalL) |
| | | { |
| | | eSize = 4; |
| | | } else |
| | |
| | | eSize = 5; |
| | | } |
| | | |
| | | if (alternateForm ||!precisionSet || (precision != 0)) |
| | | if (alternateForm || !precisionSet || (precision != 0)) |
| | | { |
| | | ca2 = new char[2 + p + eSize]; |
| | | } else |
| | |
| | | if (ca1[0] != '0') |
| | | { |
| | | ca2[0] = ca1[0]; |
| | | j = 1; |
| | | j = 1; |
| | | } else |
| | | { |
| | | for (j = 1; j < ((ePos == -1) |
| | | ? ca1.length |
| | | : ePos); j++) |
| | | ? ca1.length |
| | | : ePos); j++) |
| | | { |
| | | if (ca1[j] != '0') |
| | | { |
| | |
| | | if (((ePos != -1) && (j < ePos)) || ((ePos == -1) && (j < ca1.length))) |
| | | { |
| | | ca2[0] = ca1[j]; |
| | | expon -= j; |
| | | expon -= j; |
| | | j++; |
| | | } else |
| | | { |
| | | ca2[0] = '0'; |
| | | j = 2; |
| | | j = 2; |
| | | } |
| | | } |
| | | |
| | | if (alternateForm ||!precisionSet || (precision != 0)) |
| | | if (alternateForm || !precisionSet || (precision != 0)) |
| | | { |
| | | ca2[1] = '.'; |
| | | i = 2; |
| | | i = 2; |
| | | } else |
| | | { |
| | | i = 1; |
| | |
| | | { |
| | | switch (expon / 100) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | ca2[i] = '1'; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | ca2[i] = '2'; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | ca2[i] = '3'; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | ca2[i] = '4'; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | ca2[i] = '5'; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | ca2[i] = '6'; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | ca2[i] = '7'; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | ca2[i] = '8'; |
| | | |
| | | break; |
| | | |
| | | case 9 : |
| | | case 9: |
| | | ca2[i] = '9'; |
| | | |
| | | break; |
| | |
| | | |
| | | switch ((expon % 100) / 10) |
| | | { |
| | | case 0 : |
| | | case 0: |
| | | ca2[i] = '0'; |
| | | |
| | | break; |
| | | |
| | | case 1 : |
| | | case 1: |
| | | ca2[i] = '1'; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | ca2[i] = '2'; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | ca2[i] = '3'; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | ca2[i] = '4'; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | ca2[i] = '5'; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | ca2[i] = '6'; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | ca2[i] = '7'; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | ca2[i] = '8'; |
| | | |
| | | break; |
| | | |
| | | case 9 : |
| | | case 9: |
| | | ca2[i] = '9'; |
| | | |
| | | break; |
| | |
| | | |
| | | switch (expon % 10) |
| | | { |
| | | case 0 : |
| | | case 0: |
| | | ca2[i] = '0'; |
| | | |
| | | break; |
| | | |
| | | case 1 : |
| | | case 1: |
| | | ca2[i] = '1'; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | ca2[i] = '2'; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | ca2[i] = '3'; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | ca2[i] = '4'; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | ca2[i] = '5'; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | ca2[i] = '6'; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | ca2[i] = '7'; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | ca2[i] = '8'; |
| | | |
| | | break; |
| | | |
| | | case 9 : |
| | | case 9: |
| | | ca2[i] = '9'; |
| | | |
| | | break; |
| | |
| | | |
| | | if (thousands && (nThousands > 0)) |
| | | { |
| | | ca4 = new char[ca3.length + nThousands + lead]; |
| | | ca4 = new char[ca3.length + nThousands + lead]; |
| | | ca4[0] = ca3[0]; |
| | | |
| | | for (i = lead, k = lead; i < dp; i++) |
| | |
| | | if ((i > 0) && (dp - i) % 3 == 0) |
| | | { |
| | | // ca4[k]=','; |
| | | ca4[k] = dfs.getGroupingSeparator(); |
| | | ca4[k] = dfs.getGroupingSeparator(); |
| | | ca4[k + 1] = ca3[i]; |
| | | k += 2; |
| | | k += 2; |
| | | } else |
| | | { |
| | | ca4[k] = ca3[i]; |
| | |
| | | * Check to see if the digits that are going to |
| | | * be truncated because of the precision should |
| | | * force a round in the preceding digits. |
| | | * @param ca1 the array of digits |
| | | * |
| | | * @param ca1 the array of digits |
| | | * @param icarry the index of the first digit that |
| | | * is to be truncated from the print |
| | | * is to be truncated from the print |
| | | * @return <code>true</code> if the truncation forces |
| | | * a round that will change the print |
| | | * a round that will change the print |
| | | */ |
| | | private boolean checkForCarry(char[] ca1, int icarry) |
| | | { |
| | |
| | | if (!carry && (icarry > 0)) |
| | | { |
| | | carry = ((ca1[icarry - 1] == '1') || (ca1[icarry - 1] == '3') || (ca1[icarry - 1] == '5') |
| | | || (ca1[icarry - 1] == '7') || (ca1[icarry - 1] == '9')); |
| | | || (ca1[icarry - 1] == '7') || (ca1[icarry - 1] == '9')); |
| | | } |
| | | } |
| | | } |
| | |
| | | * is not quite finished because the symbolic |
| | | * carry may change the length of the string and |
| | | * change the exponent (in e format). |
| | | * @param cLast index of the last digit changed |
| | | * by the round |
| | | * |
| | | * @param cLast index of the last digit changed |
| | | * by the round |
| | | * @param cFirst index of the first digit allowed |
| | | * to be changed by this phase of the round |
| | | * to be changed by this phase of the round |
| | | * @return <code>true</code> if the carry forces |
| | | * a round that will change the print still |
| | | * more |
| | | * a round that will change the print still |
| | | * more |
| | | */ |
| | | private boolean startSymbolicCarry(char[] ca, int cLast, int cFirst) |
| | | { |
| | |
| | | |
| | | switch (ca[i]) |
| | | { |
| | | case '0' : |
| | | case '0': |
| | | ca[i] = '1'; |
| | | |
| | | break; |
| | | |
| | | case '1' : |
| | | case '1': |
| | | ca[i] = '2'; |
| | | |
| | | break; |
| | | |
| | | case '2' : |
| | | case '2': |
| | | ca[i] = '3'; |
| | | |
| | | break; |
| | | |
| | | case '3' : |
| | | case '3': |
| | | ca[i] = '4'; |
| | | |
| | | break; |
| | | |
| | | case '4' : |
| | | case '4': |
| | | ca[i] = '5'; |
| | | |
| | | break; |
| | | |
| | | case '5' : |
| | | case '5': |
| | | ca[i] = '6'; |
| | | |
| | | break; |
| | | |
| | | case '6' : |
| | | case '6': |
| | | ca[i] = '7'; |
| | | |
| | | break; |
| | | |
| | | case '7' : |
| | | case '7': |
| | | ca[i] = '8'; |
| | | |
| | | break; |
| | | |
| | | case '8' : |
| | | case '8': |
| | | ca[i] = '9'; |
| | | |
| | | break; |
| | | |
| | | case '9' : |
| | | case '9': |
| | | ca[i] = '0'; |
| | | carry = true; |
| | | |
| | |
| | | * the input double value is an infinity, |
| | | * not-a-number, or a finite double and formats |
| | | * each type of input appropriately. |
| | | * @param x the double value to be formatted. |
| | | * |
| | | * @param x the double value to be formatted. |
| | | * @param eChar an 'e' or 'E' to use in the |
| | | * converted double value. |
| | | * converted double value. |
| | | * @return the converted double value. |
| | | */ |
| | | private String eFormatString(double x, char eChar) |
| | | { |
| | | boolean noDigits = false; |
| | | char[] ca4, ca5; |
| | | char[] ca4, ca5; |
| | | |
| | | if (Double.isInfinite(x)) |
| | | { |
| | |
| | | |
| | | /** |
| | | * Apply zero or blank, left or right padding. |
| | | * @param ca4 array of characters before padding is |
| | | * finished |
| | | * |
| | | * @param ca4 array of characters before padding is |
| | | * finished |
| | | * @param noDigits NaN or signed Inf |
| | | * @return a padded array of characters |
| | | */ |
| | |
| | | if (nBlanks > 0) |
| | | { |
| | | ca5 = new char[ca4.length + nBlanks]; |
| | | i = 0; |
| | | j = 0; |
| | | i = 0; |
| | | j = 0; |
| | | |
| | | if (ca4[0] == '-') |
| | | { |
| | |
| | | |
| | | /** |
| | | * Format method for the f conversion character. |
| | | * |
| | | * @param x the double to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | /** |
| | | * Format method for the e or E conversion |
| | | * character. |
| | | * |
| | | * @param x the double to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | /** |
| | | * Format method for the g conversion character. |
| | | * |
| | | * <p/> |
| | | * For g format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. '+' character means that the conversion |
| | | * will always begin with a sign (+ or -). The |
| | |
| | | * ignored. The '0' flag character implies that |
| | | * padding to the field width will be done with |
| | | * zeros instead of blanks. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear after the radix character. |
| | | * Padding is with trailing 0s. |
| | | * |
| | | * @param x the double to format. |
| | | * @return the formatted String. |
| | | */ |
| | | private String printGFormat(double x) |
| | | { |
| | | String sx, sy, sz, ret; |
| | | int savePrecision = precision; |
| | | int i; |
| | | char[] ca4, ca5; |
| | | String sx, sy, sz, ret; |
| | | int savePrecision = precision; |
| | | int i; |
| | | char[] ca4, ca5; |
| | | boolean noDigits = false; |
| | | |
| | | if (Double.isInfinite(x)) |
| | |
| | | |
| | | if (conversionCharacter == 'g') |
| | | { |
| | | sx = eFormatString(x, 'e').trim(); |
| | | sx = eFormatString(x, 'e').trim(); |
| | | ePos = sx.indexOf('e'); |
| | | } else |
| | | { |
| | | sx = eFormatString(x, 'E').trim(); |
| | | sx = eFormatString(x, 'E').trim(); |
| | | ePos = sx.indexOf('E'); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // Pad with blanks or zeros. |
| | | ca5 = applyFloatPadding(ca4, false); |
| | | ca5 = applyFloatPadding(ca4, false); |
| | | precision = savePrecision; |
| | | |
| | | return new String(ca5); |
| | |
| | | /** |
| | | * Format method for the d conversion specifer and |
| | | * short argument. |
| | | * |
| | | * <p/> |
| | | * For d format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | |
| | | * ignored. The '0' flag character implies that |
| | | * padding to the field width will be done with |
| | | * zeros instead of blanks. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the short to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | /** |
| | | * Format method for the d conversion character and |
| | | * long argument. |
| | | * |
| | | * <p/> |
| | | * For d format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | |
| | | * ignored. The '0' flag character implies that |
| | | * padding to the field width will be done with |
| | | * zeros instead of blanks. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the long to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | /** |
| | | * Format method for the d conversion character and |
| | | * int argument. |
| | | * |
| | | * <p/> |
| | | * For d format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | |
| | | * ignored. The '0' flag character implies that |
| | | * padding to the field width will be done with |
| | | * zeros instead of blanks. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the int to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | /** |
| | | * Utility method for formatting using the d |
| | | * conversion character. |
| | | * |
| | | * @param sx the String to format, the result of |
| | | * converting a short, int, or long to a |
| | | * String. |
| | | * converting a short, int, or long to a |
| | | * String. |
| | | * @return the formatted String. |
| | | */ |
| | | private String printDFormat(String sx) |
| | | { |
| | | int nLeadingZeros = 0; |
| | | int nBlanks = 0, |
| | | n = 0; |
| | | int i = 0, |
| | | jFirst = 0; |
| | | boolean neg = sx.charAt(0) == '-'; |
| | | int nLeadingZeros = 0; |
| | | int nBlanks = 0, |
| | | n = 0; |
| | | int i = 0, |
| | | jFirst = 0; |
| | | boolean neg = sx.charAt(0) == '-'; |
| | | |
| | | if (sx.equals("0") && precisionSet && (precision == 0)) |
| | | { |
| | |
| | | char[] csx = sx.toCharArray(); |
| | | |
| | | jFirst = neg |
| | | ? 1 |
| | | : 0; |
| | | ? 1 |
| | | : 0; |
| | | |
| | | for (int j = 0; j < nLeadingZeros; i++, j++) |
| | | { |
| | |
| | | char[] csx = sx.toCharArray(); |
| | | |
| | | jFirst = neg |
| | | ? 1 |
| | | : 0; |
| | | ? 1 |
| | | : 0; |
| | | |
| | | for (int j = jFirst; j < csx.length; j++, i++) |
| | | { |
| | |
| | | /** |
| | | * Format method for the x conversion character and |
| | | * short argument. |
| | | * |
| | | * <p/> |
| | | * For x format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. The '#' flag character means to lead with |
| | | * '0x'. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the short to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | switch (t.length()) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | sx = "800" + t; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | sx = "80" + t; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | sx = "8" + t; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | switch (t.charAt(0)) |
| | | { |
| | | case '1' : |
| | | case '1': |
| | | sx = "9" + t.substring(1, 4); |
| | | |
| | | break; |
| | | |
| | | case '2' : |
| | | case '2': |
| | | sx = "a" + t.substring(1, 4); |
| | | |
| | | break; |
| | | |
| | | case '3' : |
| | | case '3': |
| | | sx = "b" + t.substring(1, 4); |
| | | |
| | | break; |
| | | |
| | | case '4' : |
| | | case '4': |
| | | sx = "c" + t.substring(1, 4); |
| | | |
| | | break; |
| | | |
| | | case '5' : |
| | | case '5': |
| | | sx = "d" + t.substring(1, 4); |
| | | |
| | | break; |
| | | |
| | | case '6' : |
| | | case '6': |
| | | sx = "e" + t.substring(1, 4); |
| | | |
| | | break; |
| | | |
| | | case '7' : |
| | | case '7': |
| | | sx = "f" + t.substring(1, 4); |
| | | |
| | | break; |
| | |
| | | /** |
| | | * Format method for the x conversion character and |
| | | * long argument. |
| | | * |
| | | * <p/> |
| | | * For x format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. The '#' flag character means to lead with |
| | | * '0x'. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the long to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | switch (t.length()) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | sx = "800000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | sx = "80000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | sx = "8000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | sx = "800000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | sx = "80000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | sx = "8000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | sx = "800000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | sx = "80000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 9 : |
| | | case 9: |
| | | sx = "8000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 10 : |
| | | case 10: |
| | | sx = "800000" + t; |
| | | |
| | | break; |
| | | |
| | | case 11 : |
| | | case 11: |
| | | sx = "80000" + t; |
| | | |
| | | break; |
| | | |
| | | case 12 : |
| | | case 12: |
| | | sx = "8000" + t; |
| | | |
| | | break; |
| | | |
| | | case 13 : |
| | | case 13: |
| | | sx = "800" + t; |
| | | |
| | | break; |
| | | |
| | | case 14 : |
| | | case 14: |
| | | sx = "80" + t; |
| | | |
| | | break; |
| | | |
| | | case 15 : |
| | | case 15: |
| | | sx = "8" + t; |
| | | |
| | | break; |
| | | |
| | | case 16 : |
| | | case 16: |
| | | switch (t.charAt(0)) |
| | | { |
| | | case '1' : |
| | | case '1': |
| | | sx = "9" + t.substring(1, 16); |
| | | |
| | | break; |
| | | |
| | | case '2' : |
| | | case '2': |
| | | sx = "a" + t.substring(1, 16); |
| | | |
| | | break; |
| | | |
| | | case '3' : |
| | | case '3': |
| | | sx = "b" + t.substring(1, 16); |
| | | |
| | | break; |
| | | |
| | | case '4' : |
| | | case '4': |
| | | sx = "c" + t.substring(1, 16); |
| | | |
| | | break; |
| | | |
| | | case '5' : |
| | | case '5': |
| | | sx = "d" + t.substring(1, 16); |
| | | |
| | | break; |
| | | |
| | | case '6' : |
| | | case '6': |
| | | sx = "e" + t.substring(1, 16); |
| | | |
| | | break; |
| | | |
| | | case '7' : |
| | | case '7': |
| | | sx = "f" + t.substring(1, 16); |
| | | |
| | | break; |
| | |
| | | /** |
| | | * Format method for the x conversion character and |
| | | * int argument. |
| | | * |
| | | * <p/> |
| | | * For x format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. The '#' flag character means to lead with |
| | | * '0x'. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the int to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | switch (t.length()) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | sx = "8000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | sx = "800000" + t; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | sx = "80000" + t; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | sx = "8000" + t; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | sx = "800" + t; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | sx = "80" + t; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | sx = "8" + t; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | switch (t.charAt(0)) |
| | | { |
| | | case '1' : |
| | | case '1': |
| | | sx = "9" + t.substring(1, 8); |
| | | |
| | | break; |
| | | |
| | | case '2' : |
| | | case '2': |
| | | sx = "a" + t.substring(1, 8); |
| | | |
| | | break; |
| | | |
| | | case '3' : |
| | | case '3': |
| | | sx = "b" + t.substring(1, 8); |
| | | |
| | | break; |
| | | |
| | | case '4' : |
| | | case '4': |
| | | sx = "c" + t.substring(1, 8); |
| | | |
| | | break; |
| | | |
| | | case '5' : |
| | | case '5': |
| | | sx = "d" + t.substring(1, 8); |
| | | |
| | | break; |
| | | |
| | | case '6' : |
| | | case '6': |
| | | sx = "e" + t.substring(1, 8); |
| | | |
| | | break; |
| | | |
| | | case '7' : |
| | | case '7': |
| | | sx = "f" + t.substring(1, 8); |
| | | |
| | | break; |
| | |
| | | /** |
| | | * Utility method for formatting using the x |
| | | * conversion character. |
| | | * |
| | | * @param sx the String to format, the result of |
| | | * converting a short, int, or long to a |
| | | * String. |
| | | * converting a short, int, or long to a |
| | | * String. |
| | | * @return the formatted String. |
| | | */ |
| | | private String printXFormat(String sx) |
| | | { |
| | | int nLeadingZeros = 0; |
| | | int nBlanks = 0; |
| | | int nBlanks = 0; |
| | | |
| | | if (sx.equals("0") && precisionSet && (precision == 0)) |
| | | { |
| | |
| | | n += nBlanks; |
| | | |
| | | char[] ca = new char[n]; |
| | | int i = 0; |
| | | int i = 0; |
| | | |
| | | if (leftJustify) |
| | | { |
| | |
| | | /** |
| | | * Format method for the o conversion character and |
| | | * short argument. |
| | | * |
| | | * <p/> |
| | | * For o format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. The '#' flag character means that the |
| | | * output begins with a leading 0 and the precision |
| | | * is increased by 1. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the short to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | switch (t.length()) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | sx = "10000" + t; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | sx = "1000" + t; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | sx = "100" + t; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | sx = "10" + t; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | sx = "1" + t; |
| | | |
| | | break; |
| | |
| | | /** |
| | | * Format method for the o conversion character and |
| | | * long argument. |
| | | * |
| | | * <p/> |
| | | * For o format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. The '#' flag character means that the |
| | | * output begins with a leading 0 and the precision |
| | | * is increased by 1. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the long to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | switch (t.length()) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | sx = "100000000000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | sx = "10000000000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | sx = "1000000000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | sx = "100000000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | sx = "10000000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | sx = "1000000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | sx = "100000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | sx = "10000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 9 : |
| | | case 9: |
| | | sx = "1000000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 10 : |
| | | case 10: |
| | | sx = "100000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 11 : |
| | | case 11: |
| | | sx = "10000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 12 : |
| | | case 12: |
| | | sx = "1000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 13 : |
| | | case 13: |
| | | sx = "100000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 14 : |
| | | case 14: |
| | | sx = "10000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 15 : |
| | | case 15: |
| | | sx = "1000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 16 : |
| | | case 16: |
| | | sx = "100000" + t; |
| | | |
| | | break; |
| | | |
| | | case 17 : |
| | | case 17: |
| | | sx = "10000" + t; |
| | | |
| | | break; |
| | | |
| | | case 18 : |
| | | case 18: |
| | | sx = "1000" + t; |
| | | |
| | | break; |
| | | |
| | | case 19 : |
| | | case 19: |
| | | sx = "100" + t; |
| | | |
| | | break; |
| | | |
| | | case 20 : |
| | | case 20: |
| | | sx = "10" + t; |
| | | |
| | | break; |
| | | |
| | | case 21 : |
| | | case 21: |
| | | sx = "1" + t; |
| | | |
| | | break; |
| | |
| | | /** |
| | | * Format method for the o conversion character and |
| | | * int argument. |
| | | * |
| | | * <p/> |
| | | * For o format, the flag character '-', means that |
| | | * the output should be left justified within the |
| | | * field. The default is to pad with blanks on the |
| | | * left. The '#' flag character means that the |
| | | * output begins with a leading 0 and the precision |
| | | * is increased by 1. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is to |
| | | * add no padding. Padding is with blanks by |
| | | * default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is the minimum number of |
| | | * digits to appear. Padding is with leading 0s. |
| | | * |
| | | * @param x the int to format. |
| | | * @return the formatted String. |
| | | */ |
| | |
| | | |
| | | switch (t.length()) |
| | | { |
| | | case 1 : |
| | | case 1: |
| | | sx = "2000000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 2 : |
| | | case 2: |
| | | sx = "200000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 3 : |
| | | case 3: |
| | | sx = "20000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 4 : |
| | | case 4: |
| | | sx = "2000000" + t; |
| | | |
| | | break; |
| | | |
| | | case 5 : |
| | | case 5: |
| | | sx = "200000" + t; |
| | | |
| | | break; |
| | | |
| | | case 6 : |
| | | case 6: |
| | | sx = "20000" + t; |
| | | |
| | | break; |
| | | |
| | | case 7 : |
| | | case 7: |
| | | sx = "2000" + t; |
| | | |
| | | break; |
| | | |
| | | case 8 : |
| | | case 8: |
| | | sx = "200" + t; |
| | | |
| | | break; |
| | | |
| | | case 9 : |
| | | case 9: |
| | | sx = "20" + t; |
| | | |
| | | break; |
| | | |
| | | case 10 : |
| | | case 10: |
| | | sx = "2" + t; |
| | | |
| | | break; |
| | | |
| | | case 11 : |
| | | case 11: |
| | | sx = "3" + t.substring(1); |
| | | |
| | | break; |
| | |
| | | /** |
| | | * Utility method for formatting using the o |
| | | * conversion character. |
| | | * |
| | | * @param sx the String to format, the result of |
| | | * converting a short, int, or long to a |
| | | * String. |
| | | * converting a short, int, or long to a |
| | | * String. |
| | | * @return the formatted String. |
| | | */ |
| | | private String printOFormat(String sx) |
| | | { |
| | | int nLeadingZeros = 0; |
| | | int nBlanks = 0; |
| | | int nBlanks = 0; |
| | | |
| | | if (sx.equals("0") && precisionSet && (precision == 0)) |
| | | { |
| | |
| | | nBlanks = 0; |
| | | } |
| | | |
| | | int n = nLeadingZeros + sx.length() + nBlanks; |
| | | int n = nLeadingZeros + sx.length() + nBlanks; |
| | | char[] ca = new char[n]; |
| | | int i; |
| | | int i; |
| | | |
| | | if (leftJustify) |
| | | { |
| | |
| | | /** |
| | | * Format method for the c conversion character and |
| | | * char argument. |
| | | * |
| | | * <p/> |
| | | * The only flag character that affects c format is |
| | | * the '-', meaning that the output should be left |
| | | * justified within the field. The default is to |
| | | * pad with blanks on the left. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. Padding is with |
| | | * blanks by default. The default width is 1. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, is ignored. |
| | | * |
| | | * @param x the char to format. |
| | | * @return the formatted String. |
| | | */ |
| | | private String printCFormat(char x) |
| | | { |
| | | int nPrint = 1; |
| | | int width = fieldWidth; |
| | | int width = fieldWidth; |
| | | |
| | | if (!fieldWidthSet) |
| | | { |
| | |
| | | } |
| | | |
| | | char[] ca = new char[width]; |
| | | int i = 0; |
| | | int i = 0; |
| | | |
| | | if (leftJustify) |
| | | { |
| | |
| | | /** |
| | | * Format method for the s conversion character and |
| | | * String argument. |
| | | * |
| | | * <p/> |
| | | * The only flag character that affects s format is |
| | | * the '-', meaning that the output should be left |
| | | * justified within the field. The default is to |
| | | * pad with blanks on the left. |
| | | * |
| | | * <p/> |
| | | * The field width is treated as the minimum number |
| | | * of characters to be printed. The default is the |
| | | * smaller of the number of characters in the the |
| | | * input and the precision. Padding is with blanks |
| | | * by default. |
| | | * |
| | | * <p/> |
| | | * The precision, if set, specifies the maximum |
| | | * number of characters to be printed from the |
| | | * string. A null digit string is treated |
| | | * as a 0. The default is not to set a maximum |
| | | * number of characters to be printed. |
| | | * |
| | | * @param x the String to format. |
| | | * @return the formatted String. |
| | | */ |
| | | private String printSFormat(String x) |
| | | { |
| | | int nPrint = x.length(); |
| | | int width = fieldWidth; |
| | | int width = fieldWidth; |
| | | |
| | | if (precisionSet && (nPrint > precision)) |
| | | { |
| | |
| | | } |
| | | |
| | | char[] ca = new char[n]; |
| | | int i = 0; |
| | | int i = 0; |
| | | |
| | | if (leftJustify) |
| | | { |
| | |
| | | /** |
| | | * Check for a conversion character. If it is |
| | | * there, store it. |
| | | * |
| | | * @return <code>true</code> if the conversion |
| | | * character is there, and |
| | | * <code>false</code> otherwise. |
| | | * character is there, and |
| | | * <code>false</code> otherwise. |
| | | */ |
| | | private boolean setConversionCharacter() |
| | | { |
| | |
| | | char c = fmt.charAt(pos); |
| | | |
| | | if ((c == 'i') || (c == 'd') || (c == 'f') || (c == 'g') || (c == 'G') || (c == 'o') || (c == 'x') || (c == 'X') |
| | | || (c == 'e') || (c == 'E') || (c == 'c') || (c == 's') || (c == '%')) |
| | | || (c == 'e') || (c == 'E') || (c == 'c') || (c == 's') || (c == '%')) |
| | | { |
| | | conversionCharacter = c; |
| | | pos++; |
| | |
| | | if (!setPrecisionArgPosition()) |
| | | { |
| | | variablePrecision = true; |
| | | precisionSet = true; |
| | | precisionSet = true; |
| | | } |
| | | |
| | | return; |
| | |
| | | { |
| | | String sz = fmt.substring(firstPos + 1, pos); |
| | | |
| | | precision = Integer.parseInt(sz); |
| | | precision = Integer.parseInt(sz); |
| | | precisionSet = true; |
| | | } |
| | | } |
| | |
| | | { |
| | | int firstPos = pos; |
| | | |
| | | fieldWidth = 0; |
| | | fieldWidth = 0; |
| | | fieldWidthSet = false; |
| | | |
| | | if ((pos < fmt.length()) && (fmt.charAt(pos) == '*')) |
| | |
| | | if (!setFieldWidthArgPosition()) |
| | | { |
| | | variableFieldWidth = true; |
| | | fieldWidthSet = true; |
| | | fieldWidthSet = true; |
| | | } |
| | | } else |
| | | { |
| | |
| | | { |
| | | String sz = fmt.substring(firstPos, pos); |
| | | |
| | | fieldWidth = Integer.parseInt(sz); |
| | | fieldWidth = Integer.parseInt(sz); |
| | | fieldWidthSet = true; |
| | | } |
| | | } |
| | |
| | | if (fmt.charAt(xPos) == '$') |
| | | { |
| | | positionalSpecification = true; |
| | | argumentPosition = Integer.parseInt(fmt.substring(pos, xPos)); |
| | | pos = xPos + 1; |
| | | argumentPosition = Integer.parseInt(fmt.substring(pos, xPos)); |
| | | pos = xPos + 1; |
| | | } |
| | | } |
| | | } |
| | |
| | | private boolean setFieldWidthArgPosition() |
| | | { |
| | | boolean ret = false; |
| | | int xPos; |
| | | int xPos; |
| | | |
| | | for (xPos = pos; xPos < fmt.length(); xPos++) |
| | | { |
| | |
| | | { |
| | | if (fmt.charAt(xPos) == '$') |
| | | { |
| | | positionalFieldWidth = true; |
| | | positionalFieldWidth = true; |
| | | argumentPositionForFieldWidth = Integer.parseInt(fmt.substring(pos, xPos)); |
| | | pos = xPos + 1; |
| | | ret = true; |
| | | pos = xPos + 1; |
| | | ret = true; |
| | | } |
| | | } |
| | | |
| | |
| | | private boolean setPrecisionArgPosition() |
| | | { |
| | | boolean ret = false; |
| | | int xPos; |
| | | int xPos; |
| | | |
| | | for (xPos = pos; xPos < fmt.length(); xPos++) |
| | | { |
| | |
| | | { |
| | | if (fmt.charAt(xPos) == '$') |
| | | { |
| | | positionalPrecision = true; |
| | | positionalPrecision = true; |
| | | argumentPositionForPrecision = Integer.parseInt(fmt.substring(pos, xPos)); |
| | | pos = xPos + 1; |
| | | ret = true; |
| | | pos = xPos + 1; |
| | | ret = true; |
| | | } |
| | | } |
| | | |
| | |
| | | private void setFlagCharacters() |
| | | { |
| | | /* '-+ #0 */ |
| | | thousands = false; |
| | | leftJustify = false; |
| | | leadingSign = false; |
| | | leadingSpace = false; |
| | | thousands = false; |
| | | leftJustify = false; |
| | | leadingSign = false; |
| | | leadingSpace = false; |
| | | alternateForm = false; |
| | | leadingZeros = false; |
| | | leadingZeros = false; |
| | | |
| | | for (; pos < fmt.length(); pos++) |
| | | { |
| | |
| | | thousands = true; |
| | | } else if (c == '-') |
| | | { |
| | | leftJustify = true; |
| | | leftJustify = true; |
| | | leadingZeros = false; |
| | | } else if (c == '+') |
| | | { |
| | | leadingSign = true; |
| | | leadingSign = true; |
| | | leadingSpace = false; |
| | | } else if (c == ' ') |
| | | { |