📄 printfformat.java
字号:
ca5 = new char[ca4.length + nZeros]; if (!minusSign) { if (leadingSign) ca5[0] = '+'; if (leadingSpace) ca5[0] = ' '; } else ca5[0] = '-'; for (i = 0; i < nZeros; i++, j++) ca5[j] = '0'; for (i = 0; i < ca4.length; i++, j++) ca5[j] = ca4[i]; int lead = 0; if (ca5[0] == '+' || ca5[0] == '-' || ca5[0] == ' ') lead = 1; int dp = lead; for (; dp < ca5.length; dp++) if (ca5[dp] == '.') break; int nThousands = (dp - lead) / 3; // Localize the decimal point. if (dp < ca5.length) ca5[dp] = dfs.getDecimalSeparator(); char[] ca6 = ca5; if (thousands && nThousands > 0) { 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 + 1] = ca5[i]; k += 2; } else { ca6[k] = ca5[i]; k++; } } for (; i < ca5.length; i++, k++) { ca6[k] = ca5[i]; } } return ca6; } /** * An intermediate routine on the way to creating * an f 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. * @return the converted double value. */ private String fFormatString(double x) { boolean noDigits = false; char[] ca6, ca7; if (Double.isInfinite(x)) { if (x == Double.POSITIVE_INFINITY) { if (leadingSign) ca6 = "+Inf".toCharArray(); else if (leadingSpace) ca6 = " Inf".toCharArray(); else ca6 = "Inf".toCharArray(); } else ca6 = "-Inf".toCharArray(); noDigits = true; } else if (Double.isNaN(x)) { if (leadingSign) ca6 = "+NaN".toCharArray(); else if (leadingSpace) ca6 = " NaN".toCharArray(); else ca6 = "NaN".toCharArray(); noDigits = true; } else ca6 = fFormatDigits(x); ca7 = applyFloatPadding(ca6, false); return new String(ca7); } /** * For e 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. * * 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 * formats when the optional L is used before the * e, E, g, or G conversion character. The optional * L does not imply conversion to a long long * double. */ private char[] eFormatDigits(double x, char eChar) { 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; 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); } } ePos = sx.indexOf('E'); if (ePos == -1) ePos = sx.indexOf('e'); 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)); } } if (rPos != -1) expon += rPos - 1; if (precisionSet) p = precision; else p = defaultDigits - 1; if (rPos != -1 && ePos != -1) ca1 = (sx.substring(0, rPos) + sx.substring(rPos + 1, ePos)).toCharArray(); else if (rPos != -1) ca1 = (sx.substring(0, rPos) + sx.substring(rPos + 1)).toCharArray(); else if (ePos != -1) ca1 = sx.substring(0, ePos).toCharArray(); else ca1 = sx.toCharArray(); boolean carry = false; int i0 = 0; if (ca1[0] != '0') i0 = 0; else for (i0 = 0; i0 < ca1.length; i0++) if (ca1[i0] != '0') break; if (i0 + p < ca1.length - 1) { carry = checkForCarry(ca1, i0 + p + 1); if (carry) carry = startSymbolicCarry(ca1, i0 + p, i0); if (carry) { ca2 = new char[i0 + p + 1]; ca2[i0] = '1'; for (j = 0; j < i0; j++) ca2[j] = '0'; for (i = i0, j = i0 + 1; j < p + 1; i++, j++) ca2[j] = ca1[i]; expon++; ca1 = ca2; } } if (Math.abs(expon) < 100 && !optionalL) eSize = 4; else eSize = 5; if (alternateForm || !precisionSet || precision != 0) ca2 = new char[2 + p + eSize]; else ca2 = new char[1 + eSize]; if (ca1[0] != '0') { ca2[0] = ca1[0]; j = 1; } else { for (j = 1; j < (ePos == -1 ? ca1.length : ePos); j++) if (ca1[j] != '0') break; if ((ePos != -1 && j < ePos) || (ePos == -1 && j < ca1.length)) { ca2[0] = ca1[j]; expon -= j; j++; } else { ca2[0] = '0'; j = 2; } } if (alternateForm || !precisionSet || precision != 0) { ca2[1] = '.'; i = 2; } else i = 1; for (k = 0; k < p && j < ca1.length; j++, i++, k++) ca2[i] = ca1[j]; for (; i < ca2.length - eSize; i++) ca2[i] = '0'; ca2[i++] = eChar; if (expon < 0) ca2[i++] = '-'; else ca2[i++] = '+'; expon = Math.abs(expon); if (expon >= 100) { switch (expon / 100) { case 1 : ca2[i] = '1'; break; case 2 : ca2[i] = '2'; break; case 3 : ca2[i] = '3'; break; case 4 : ca2[i] = '4'; break; case 5 : ca2[i] = '5'; break; case 6 : ca2[i] = '6'; break; case 7 : ca2[i] = '7'; break; case 8 : ca2[i] = '8'; break; case 9 : ca2[i] = '9'; break; } i++; } switch ((expon % 100) / 10) { case 0 : ca2[i] = '0'; break; case 1 : ca2[i] = '1'; break; case 2 : ca2[i] = '2'; break; case 3 : ca2[i] = '3'; break; case 4 : ca2[i] = '4'; break; case 5 : ca2[i] = '5'; break; case 6 : ca2[i] = '6'; break; case 7 : ca2[i] = '7'; break; case 8 : ca2[i] = '8'; break; case 9 : ca2[i] = '9'; break; } i++; switch (expon % 10) { case 0 : ca2[i] = '0'; break; case 1 : ca2[i] = '1'; break; case 2 : ca2[i] = '2'; break; case 3 : ca2[i] = '3'; break; case 4 : ca2[i] = '4'; break; case 5 : ca2[i] = '5'; break; case 6 : ca2[i] = '6'; break; case 7 : ca2[i] = '7'; break; case 8 : ca2[i] = '8'; break; case 9 : ca2[i] = '9'; break; } int nZeros = 0; if (!leftJustify && leadingZeros) { int xThousands = 0; if (thousands) { int xlead = 0; if (ca2[0] == '+' || ca2[0] == '-' || ca2[0] == ' ') xlead = 1; int xdp = xlead; for (; xdp < ca2.length; xdp++) if (ca2[xdp] == '.') break; xThousands = (xdp - xlead) / 3; } if (fieldWidthSet) nZeros = fieldWidth - ca2.length; if ((!minusSign && (leadingSign || leadingSpace)) || minusSign) nZeros--; nZeros -= xThousands; if (nZeros < 0) nZeros = 0; } j = 0; if ((!minusSign && (leadingSign || leadingSpace)) || minusSign) { ca3 = new char[ca2.length + nZeros + 1]; j++; } else ca3 = new char[ca2.length + nZeros]; if (!minusSign) { if (leadingSign) ca3[0] = '+'; if (leadingSpace) ca3[0] = ' '; } else ca3[0] = '-'; for (k = 0; k < nZeros; j++, k++) ca3[j] = '0'; for (i = 0; i < ca2.length && j < ca3.length; i++, j++) ca3[j] = ca2[i]; int lead = 0; if (ca3[0] == '+' || ca3[0] == '-' || ca3[0] == ' ') lead = 1; int dp = lead; for (; dp < ca3.length; dp++) if (ca3[dp] == '.') break; int nThousands = dp / 3; // Localize the decimal point. if (dp < ca3.length) ca3[dp] = dfs.getDecimalSeparator(); char[] ca4 = ca3; if (thousands && nThousands > 0) { 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 + 1] = ca3[i]; k += 2; } else { ca4[k] = ca3[i]; k++; } } for (; i < ca3.length; i++, k++) ca4[k] = ca3[i]; } return ca4; } /** * 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 icarry the index of the first digit that * is to be truncated from the print * @return <code>true</code> if the truncation forces * a round that will change the print */ private boolean checkForCarry(char[] ca1, int icarry) { boolean carry = false; if (icarry < ca1.length) { if (ca1[icarry] == '6' || ca1[icarry] == '7' || ca1[icarry] == '8' || ca1[icarry] == '9') carry = true; else if (ca1[icarry] == '5') { int ii = icarry + 1; for (; ii < ca1.length; ii++) if (ca1[ii] != '0') break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -