📄 printfformat.java
字号:
} /** * Get the String for this instance. Translate * any escape sequences. * * @return s the stored String. */ String getLiteral() { StringBuffer sb = new StringBuffer(); int i = 0; while (i < fmt.length()) { if (fmt.charAt(i) == '\\') { i++; if (i < fmt.length()) { char c = fmt.charAt(i); switch (c) { case 'a' : sb.append((char) 0x07); break; case 'b' : sb.append('\b'); break; case 'f' : sb.append('\f'); break; case 'n' : sb.append(System.getProperty("line.separator")); break; case 'r' : sb.append('\r'); break; case 't' : sb.append('\t'); break; case 'v' : sb.append((char) 0x0b); break; case '\\' : sb.append('\\'); break; } i++; } else sb.append('\\'); } else i++; } return fmt; } /** * Get the conversion character that tells what * type of control character this instance has. * * @return the conversion character. */ char getConversionCharacter() { return conversionCharacter; } /** * 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>. */ boolean isVariableFieldWidth() { return variableFieldWidth; } /** * 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) { if (fw < 0) leftJustify = true; fieldWidthSet = true; 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>. */ boolean isVariablePrecision() { return variablePrecision; } /** * 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); } /** * Format an int argument using this conversion * specification. * @param s the int to format. * @return the formatted String. * @exception IllegalArgumentException if the * conversion character is f, e, E, g, or G. */ String internalsprintf(int s) throws IllegalArgumentException { String s2 = ""; switch (conversionCharacter) { case 'd' : case 'i' : if (optionalh) s2 = printDFormat((short) s); else if (optionall) s2 = printDFormat((long) s); else s2 = printDFormat(s); break; case 'x' : case 'X' : if (optionalh) s2 = printXFormat((short) s); else if (optionall) s2 = printXFormat((long) s); else s2 = printXFormat(s); break; case 'o' : if (optionalh) s2 = printOFormat((short) s); else if (optionall) s2 = printOFormat((long) s); else s2 = printOFormat(s); break; case 'c' : case 'C' : s2 = printCFormat((char) s); break; default : throw new IllegalArgumentException( "Cannot format a int with a format using a " + conversionCharacter + " 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. */ String internalsprintf(long s) throws IllegalArgumentException { String s2 = ""; switch (conversionCharacter) { case 'd' : case 'i' : if (optionalh) s2 = printDFormat((short) s); else if (optionall) s2 = printDFormat(s); else s2 = printDFormat((int) s); break; case 'x' : case 'X' : if (optionalh) s2 = printXFormat((short) s); else if (optionall) s2 = printXFormat(s); else s2 = printXFormat((int) s); break; case 'o' : if (optionalh) s2 = printOFormat((short) s); else if (optionall) s2 = printOFormat(s); else s2 = printOFormat((int) s); break; case 'c' : case 'C' : s2 = printCFormat((char) s); break; default : throw new IllegalArgumentException( "Cannot format a long with a format using a " + conversionCharacter + " 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. */ String internalsprintf(double s) throws IllegalArgumentException { String s2 = ""; switch (conversionCharacter) { case 'f' : s2 = printFFormat(s); break; case 'E' : case 'e' : s2 = printEFormat(s); break; case 'G' : case 'g' : s2 = printGFormat(s); break; default : throw new IllegalArgumentException( "Cannot " + "format a double with a format using a " + conversionCharacter + " 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. */ String internalsprintf(String s) throws IllegalArgumentException { String s2 = ""; if (conversionCharacter == 's' || conversionCharacter == 'S') s2 = printSFormat(s); else throw new IllegalArgumentException( "Cannot " + "format a String with a format using a " + conversionCharacter + " 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. */ String internalsprintf(Object s) { String s2 = ""; if (conversionCharacter == 's' || conversionCharacter == 'S') s2 = printSFormat(s.toString()); else throw new IllegalArgumentException( "Cannot format a String with a format using" + " a " + conversionCharacter + " conversion character."); return s2; } /** * For f 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. '+' character means that the conversion * will always begin with a sign (+ or -). The * blank flag character means that a non-negative * input will be preceded with a blank. If both * a '+' and a ' ' are specified, the blank flag * is ignored. The '0' flag character implies that * padding to the field width will be done with * zeros instead of blanks. * * 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. * * 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; boolean minusSign = false; if (x > 0.0) sx = Double.toString(x); else if (x < 0.0) { sx = Double.toString(-x); minusSign = true; } else { sx = Double.toString(x); if (sx.charAt(0) == '-') { minusSign = true; sx = sx.substring(1); } } int ePos = sx.indexOf('E'); int rPos = sx.indexOf('.'); if (rPos != -1) n1In = rPos; else if (ePos != -1) n1In = ePos; else n1In = sx.length(); if (rPos != -1) { if (ePos != -1) n2In = ePos - rPos - 1; else n2In = sx.length() - rPos - 1; } else n2In = 0; if (ePos != -1) { int ie = ePos + 1; expon = 0; if (sx.charAt(ie) == '-') { for (++ie; ie < sx.length(); ie++) if (sx.charAt(ie) != '0') break; if (ie < sx.length()) expon = -Integer.parseInt(sx.substring(ie)); } else { if (sx.charAt(ie) == '+') ++ie; for (; ie < sx.length(); ie++) if (sx.charAt(ie) != '0') break; if (ie < sx.length()) expon = Integer.parseInt(sx.substring(ie)); } } int p; if (precisionSet) p = precision; else p = defaultDigits - 1; char[] ca1 = sx.toCharArray(); char[] ca2 = new char[n1In + n2In]; char[] ca3, ca4, ca5; for (j = 0; j < n1In; j++) ca2[j] = ca1[j]; i = j + 1; for (k = 0; k < n2In; j++, i++, k++) ca2[j] = ca1[i]; if (n1In + expon <= 0) { ca3 = new char[-expon + n2In]; for (j = 0, k = 0; k < (-n1In - expon); k++, j++) ca3[j] = '0'; for (i = 0; i < (n1In + n2In); i++, j++) ca3[j] = ca2[i]; } else ca3 = ca2; boolean carry = false; if (p < -expon + n2In) { if (expon < 0) i = p; else i = p + n1In; carry = checkForCarry(ca3, i); if (carry) carry = startSymbolicCarry(ca3, i - 1, 0); } if (n1In + expon <= 0) { ca4 = new char[2 + p]; if (!carry) ca4[0] = '0'; else ca4[0] = '1'; if (alternateForm || !precisionSet || precision != 0) { ca4[1] = '.'; for (i = 0, j = 2; i < Math.min(p, ca3.length); i++, j++) ca4[j] = ca3[i]; for (; j < ca4.length; j++) ca4[j] = '0'; } } else { if (!carry) { if (alternateForm || !precisionSet || precision != 0) ca4 = new char[n1In + expon + p + 1]; else ca4 = new char[n1In + expon]; j = 0; } else { if (alternateForm || !precisionSet || precision != 0) ca4 = new char[n1In + expon + p + 2]; else ca4 = new char[n1In + expon + 1]; ca4[0] = '1'; j = 1; } for (i = 0; i < Math.min(n1In + expon, ca3.length); i++, j++) ca4[j] = ca3[i]; for (; i < n1In + expon; i++, j++) ca4[j] = '0'; if (alternateForm || !precisionSet || precision != 0) { ca4[j] = '.'; j++; for (k = 0; i < ca3.length && k < p; i++, j++, k++) ca4[j] = ca3[i]; for (; j < ca4.length; j++) ca4[j] = '0'; } } int nZeros = 0; if (!leftJustify && leadingZeros) { int xThousands = 0; if (thousands) { int xlead = 0; if (ca4[0] == '+' || ca4[0] == '-' || ca4[0] == ' ') xlead = 1; int xdp = xlead; for (; xdp < ca4.length; xdp++) if (ca4[xdp] == '.') break; xThousands = (xdp - xlead) / 3; } if (fieldWidthSet) nZeros = fieldWidth - ca4.length; if ((!minusSign && (leadingSign || leadingSpace)) || minusSign) nZeros--; nZeros -= xThousands; if (nZeros < 0) nZeros = 0; } j = 0; if ((!minusSign && (leadingSign || leadingSpace)) || minusSign) { ca5 = new char[ca4.length + nZeros + 1]; j++; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -