📄 printfformat.java
字号:
* negative precision will be changed to zero.
* @param pr the precision.
*/
void setPrecisionWithArg(int pr) {
m_precisionSet = true;
m_precision = Math.max(pr, 0);
}
/**
* 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 (m_fieldWidthSet) {
int i, j, nBlanks;
if (m_leftJustify) {
nBlanks = m_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 (!m_leadingZeros || noDigits) {
nBlanks = m_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 (m_leadingZeros) {
nBlanks = m_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;
}
/**
* 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;
}
}
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;
}
/**
* 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.
*
* @param x the x parameter
* @param eChar the eChar parameter
* @return the result
*/
private char[] eFormatDigits(double x, char eChar) {
char[] ca1, ca2, ca3;
// int defaultDigits=6;
String sx;
int i, j, k, p;
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 (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 (m_precisionSet) {
p = m_precision;
} else {
p = DEFAULT_DIGITS - 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 && !m_optionalL) {
eSize = 4;
} else {
eSize = 5;
}
if (m_alternateForm || !m_precisionSet || m_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 (m_alternateForm || !m_precisionSet || m_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;
default:
// noop
}
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;
default:
// noop
}
i++;
switch (expon % 10) {
case 0:
ca2[i] = '0';
break;
case 1:
ca2[i] = '1';
break;
case 2:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -