📄 printfformat.java
字号:
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
}
int nZeros = 0;
if (!m_leftJustify && m_leadingZeros) {
int xThousands = 0;
if (m_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 (m_fieldWidthSet) {
nZeros = m_fieldWidth - ca2.length;
}
if ((!minusSign && (m_leadingSign || m_leadingSpace)) || minusSign) {
nZeros--;
}
nZeros -= xThousands;
if (nZeros < 0) {
nZeros = 0;
}
}
j = 0;
if ((!minusSign && (m_leadingSign || m_leadingSpace)) || minusSign) {
ca3 = new char[ca2.length + nZeros + 1];
j++;
} else {
ca3 = new char[ca2.length + nZeros];
}
if (!minusSign) {
if (m_leadingSign) {
ca3[0] = '+';
}
if (m_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] = m_dfs.getDecimalSeparator();
}
char[] ca4 = ca3;
if (m_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] = m_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;
}
/**
* 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) {
char[] ca4, ca5;
if (Double.isInfinite(x)) {
if (x == Double.POSITIVE_INFINITY) {
if (m_leadingSign) {
ca4 = "+Inf".toCharArray();
} else if (m_leadingSpace) {
ca4 = " Inf".toCharArray();
} else {
ca4 = "Inf".toCharArray();
}
} else {
ca4 = "-Inf".toCharArray();
}
} else if (Double.isNaN(x)) {
if (m_leadingSign) {
ca4 = "+NaN".toCharArray();
} else if (m_leadingSpace) {
ca4 = " NaN".toCharArray();
} else {
ca4 = "NaN".toCharArray();
}
} else {
ca4 = eFormatDigits(x, eChar);
}
ca5 = applyFloatPadding(ca4, false);
return new String(ca5);
}
/**
* 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.
*
* @param x the paramater
* @return the result
*/
private char[] fFormatDigits(double x) {
// int defaultDigits=6;
String sx;
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 (m_precisionSet) {
p = m_precision;
} else {
p = DEFAULT_DIGITS - 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 (m_alternateForm || !m_precisionSet || m_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 (m_alternateForm || !m_precisionSet || m_precision != 0) {
ca4 = new char[n1In + expon + p + 1];
} else {
ca4 = new char[n1In + expon];
}
j = 0;
} else {
if (m_alternateForm || !m_precisionSet || m_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 (m_alternateForm || !m_precisionSet || m_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 (!m_leftJustify && m_leadingZeros) {
int xThousands = 0;
if (m_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 (m_fieldWidthSet) {
nZeros = m_fieldWidth - ca4.length;
}
if ((!minusSign && (m_leadingSign || m_leadingSpace)) || minusSign) {
nZeros--;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -