📄 printfformat.java
字号:
carry = ii < ca1.length; 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'); } } } return carry; } /** * Start the symbolic carry process. The process * 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 cFirst index of the first digit allowed * 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 */ private boolean startSymbolicCarry(char[] ca, int cLast, int cFirst) { boolean carry = true; for (int i = cLast; carry && i >= cFirst; i--) { carry = false; switch (ca[i]) { case '0' : ca[i] = '1'; break; case '1' : ca[i] = '2'; break; case '2' : ca[i] = '3'; break; case '3' : ca[i] = '4'; break; case '4' : ca[i] = '5'; break; case '5' : ca[i] = '6'; break; case '6' : ca[i] = '7'; break; case '7' : ca[i] = '8'; break; case '8' : ca[i] = '9'; break; case '9' : ca[i] = '0'; carry = true; break; } } return carry; } /** * An intermediate routine on the way to creating * an e format String. The method decides whether * 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 eChar an 'e' or 'E' to use in the * converted double value. * @return the converted double value. */ private String eFormatString(double x, char eChar) { boolean noDigits = false; char[] ca4, ca5; if (Double.isInfinite(x)) { if (x == Double.POSITIVE_INFINITY) { if (leadingSign) ca4 = "+Inf".toCharArray(); else if (leadingSpace) ca4 = " Inf".toCharArray(); else ca4 = "Inf".toCharArray(); } else ca4 = "-Inf".toCharArray(); noDigits = true; } else if (Double.isNaN(x)) { if (leadingSign) ca4 = "+NaN".toCharArray(); else if (leadingSpace) ca4 = " NaN".toCharArray(); else ca4 = "NaN".toCharArray(); noDigits = true; } else ca4 = eFormatDigits(x, eChar); ca5 = applyFloatPadding(ca4, false); return new String(ca5); } /** * Apply zero or blank, left or right padding. * @param ca4 array of characters before padding is * finished * @param noDigits NaN or signed Inf * @return a padded array of characters */ private char[] applyFloatPadding(char[] ca4, boolean noDigits) { char[] ca5 = ca4; if (fieldWidthSet) { int i, j, nBlanks; if (leftJustify) { nBlanks = fieldWidth - ca4.length; if (nBlanks > 0) { ca5 = new char[ca4.length + nBlanks]; for (i = 0; i < ca4.length; i++) ca5[i] = ca4[i]; for (j = 0; j < nBlanks; j++, i++) ca5[i] = ' '; } } else if (!leadingZeros || noDigits) { nBlanks = fieldWidth - ca4.length; if (nBlanks > 0) { ca5 = new char[ca4.length + nBlanks]; for (i = 0; i < nBlanks; i++) ca5[i] = ' '; for (j = 0; j < ca4.length; i++, j++) ca5[i] = ca4[j]; } } else if (leadingZeros) { nBlanks = fieldWidth - ca4.length; if (nBlanks > 0) { ca5 = new char[ca4.length + nBlanks]; i = 0; j = 0; if (ca4[0] == '-') { ca5[0] = '-'; i++; j++; } for (int k = 0; k < nBlanks; i++, k++) ca5[i] = '0'; for (; j < ca4.length; i++, j++) ca5[i] = ca4[j]; } } } return ca5; } /** * Format method for the f conversion character. * @param x the double to format. * @return the formatted String. */ private String printFFormat(double x) { return fFormatString(x); } /** * Format method for the e or E conversion * character. * @param x the double to format. * @return the formatted String. */ private String printEFormat(double x) { if (conversionCharacter == 'e') return eFormatString(x, 'e'); else return eFormatString(x, 'E'); } /** * Format method for the g conversion character. * * For g 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 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; boolean noDigits = false; if (Double.isInfinite(x)) { if (x == Double.POSITIVE_INFINITY) { if (leadingSign) ca4 = "+Inf".toCharArray(); else if (leadingSpace) ca4 = " Inf".toCharArray(); else ca4 = "Inf".toCharArray(); } else ca4 = "-Inf".toCharArray(); noDigits = true; } else if (Double.isNaN(x)) { if (leadingSign) ca4 = "+NaN".toCharArray(); else if (leadingSpace) ca4 = " NaN".toCharArray(); else ca4 = "NaN".toCharArray(); noDigits = true; } else { if (!precisionSet) precision = defaultDigits; if (precision == 0) precision = 1; int ePos = -1; if (conversionCharacter == 'g') { sx = eFormatString(x, 'e').trim(); ePos = sx.indexOf('e'); } else { sx = eFormatString(x, 'E').trim(); ePos = sx.indexOf('E'); } i = ePos + 1; int expon = 0; if (sx.charAt(i) == '-') { for (++i; i < sx.length(); i++) if (sx.charAt(i) != '0') break; if (i < sx.length()) expon = -Integer.parseInt(sx.substring(i)); } else { if (sx.charAt(i) == '+') ++i; for (; i < sx.length(); i++) if (sx.charAt(i) != '0') break; if (i < sx.length()) expon = Integer.parseInt(sx.substring(i)); } // Trim trailing zeros. // If the radix character is not followed by // a digit, trim it, too. if (!alternateForm) { if (expon >= -4 && expon < precision) sy = fFormatString(x).trim(); else sy = sx.substring(0, ePos); i = sy.length() - 1; for (; i >= 0; i--) if (sy.charAt(i) != '0') break; if (i >= 0 && sy.charAt(i) == '.') i--; if (i == -1) sz = "0"; else if (!Character.isDigit(sy.charAt(i))) sz = sy.substring(0, i + 1) + "0"; else sz = sy.substring(0, i + 1); if (expon >= -4 && expon < precision) ret = sz; else ret = sz + sx.substring(ePos); } else { if (expon >= -4 && expon < precision) ret = fFormatString(x).trim(); else ret = sx; } // leading space was trimmed off during // construction if (leadingSpace) if (x >= 0) ret = " " + ret; ca4 = ret.toCharArray(); } // Pad with blanks or zeros. ca5 = applyFloatPadding(ca4, false); precision = savePrecision; return new String(ca5); } /** * Format method for the d conversion specifer and * short argument. * * 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 * left. A '+' 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 minimum number of * digits to appear. Padding is with leading 0s. * @param x the short to format. * @return the formatted String. */ private String printDFormat(short x) { return printDFormat(Short.toString(x)); } /** * Format method for the d conversion character and * long argument. * * 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 * left. A '+' 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 minimum number of * digits to appear. Padding is with leading 0s. * @param x the long to format. * @return the formatted String. */ private String printDFormat(long x) { return printDFormat(Long.toString(x)); } /** * Format method for the d conversion character and * int argument. * * 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 * left. A '+' 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 minimum number of * digits to appear. Padding is with leading 0s. * @param x the int to format. * @return the formatted String. */ private String printDFormat(int x) { return printDFormat(Integer.toString(x)); } /** * 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. * @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) == '-'; if (sx.equals("0") && precisionSet && precision == 0) sx = ""; if (!neg) { if (precisionSet && sx.length() < precision) nLeadingZeros = precision - sx.length(); } else { if (precisionSet && (sx.length() - 1) < precision) nLeadingZeros = precision - sx.length() + 1; } if (nLeadingZeros < 0) nLeadingZeros = 0; if (fieldWidthSet) { nBlanks = fieldWidth - nLeadingZeros - sx.length(); if (!neg && (leadingSign || leadingSpace)) nBlanks--; } if (nBlanks < 0) nBlanks = 0; if (leadingSign) n++; else if (leadingSpace) n++; n += nBlanks; n += nLeadingZeros; n += sx.length(); char[] ca = new char[n]; if (leftJustify) { if (neg) ca[i++] = '-'; else if (leadingSign) ca[i++] = '+'; else if (leadingSpace) ca[i++] = ' '; char[]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -