?? format.java
字號:
if (precision < 0) precision = 6;
int s = 1;
if (x < 0) { x = -x; s = -1; }
if (Double.isNaN(x)) r = "NaN";
else if (x == Double.POSITIVE_INFINITY) r = "Inf";
else if (fmt == 'f')
r = fixedFormat(x);
else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G')
r = expFormat(x);
else throw new java.lang.IllegalArgumentException();
return pad(sign(s, r));
}
/**
Formats an integer into a string (like sprintf in C)
@param x the number to format
@return the formatted string
*/
public String format(int x)
{
long lx = x;
if (fmt == 'o' || fmt == 'x' || fmt == 'X')
lx &= 0xFFFFFFFFL;
return format(lx);
}
/**
Formats a long integer into a string (like sprintf in C)
@param x the number to format
@return the formatted string
*/
public String format(long x)
{
String r;
int s = 0;
if (fmt == 'd' || fmt == 'i')
{
if (x < 0)
{
r = ("" + x).substring(1);
s = -1;
}
else
{
r = "" + x;
s = 1;
}
}
else if (fmt == 'o')
r = convert(x, 3, 7, "01234567");
else if (fmt == 'x')
r = convert(x, 4, 15, "0123456789abcdef");
else if (fmt == 'X')
r = convert(x, 4, 15, "0123456789ABCDEF");
else throw new java.lang.IllegalArgumentException();
return pad(sign(s, r));
}
/**
Formats a character into a string (like sprintf in C)
@param x the value to format
@return the formatted string
*/
public String format(char c)
{
if (fmt != 'c')
throw new java.lang.IllegalArgumentException();
String r = "" + c;
return pad(r);
}
/**
Formats a string into a larger string (like sprintf in C)
@param x the value to format
@return the formatted string
*/
public String format(String s)
{
if (fmt != 's')
throw new java.lang.IllegalArgumentException();
if (precision >= 0 && precision < s.length())
s = s.substring(0, precision);
return pad(s);
}
/**
a test stub for the format class
*/
public static void main(String[] a)
{
double x = 1.23456789012;
double y = 123;
double z = 1.2345e30;
double w = 1.02;
double u = 1.234e-5;
int d = 0xCAFE;
Format.printf("x = |%f|\n", x);
Format.printf("u = |%20f|\n", u);
Format.printf("x = |% .5f|\n", x);
Format.printf("w = |%20.5f|\n", w);
Format.printf("x = |%020.5f|\n", x);
Format.printf("x = |%+20.5f|\n", x);
Format.printf("x = |%+020.5f|\n", x);
Format.printf("x = |% 020.5f|\n", x);
Format.printf("y = |%#+20.5f|\n", y);
Format.printf("y = |%-+20.5f|\n", y);
Format.printf("z = |%20.5f|\n", z);
Format.printf("x = |%e|\n", x);
Format.printf("u = |%20e|\n", u);
Format.printf("x = |% .5e|\n", x);
Format.printf("w = |%20.5e|\n", w);
Format.printf("x = |%020.5e|\n", x);
Format.printf("x = |%+20.5e|\n", x);
Format.printf("x = |%+020.5e|\n", x);
Format.printf("x = |% 020.5e|\n", x);
Format.printf("y = |%#+20.5e|\n", y);
Format.printf("y = |%-+20.5e|\n", y);
Format.printf("x = |%g|\n", x);
Format.printf("z = |%g|\n", z);
Format.printf("w = |%g|\n", w);
Format.printf("u = |%g|\n", u);
Format.printf("y = |%.2g|\n", y);
Format.printf("y = |%#.2g|\n", y);
Format.printf("d = |%d|\n", d);
Format.printf("d = |%20d|\n", d);
Format.printf("d = |%020d|\n", d);
Format.printf("d = |%+20d|\n", d);
Format.printf("d = |% 020d|\n", d);
Format.printf("d = |%-20d|\n", d);
Format.printf("d = |%20.8d|\n", d);
Format.printf("d = |%x|\n", d);
Format.printf("d = |%20X|\n", d);
Format.printf("d = |%#20x|\n", d);
Format.printf("d = |%020X|\n", d);
Format.printf("d = |%20.8x|\n", d);
Format.printf("d = |%o|\n", d);
Format.printf("d = |%020o|\n", d);
Format.printf("d = |%#20o|\n", d);
Format.printf("d = |%#020o|\n", d);
Format.printf("d = |%20.12o|\n", d);
Format.printf("s = |%-20s|\n", "Hello");
Format.printf("s = |%-20c|\n", '!');
// regression test to confirm fix of reported bugs
Format.printf("|%i|\n", Long.MIN_VALUE);
Format.printf("|%6.2e|\n", 0.0);
Format.printf("|%6.2g|\n", 0.0);
Format.printf("|%6.2f|\n", 9.99);
Format.printf("|%6.2f|\n", 9.999);
Format.printf("|%.2f|\n", 1.999);
Format.printf("|%6.0f|\n", 9.999);
Format.printf("|%20.10s|\n", "Hello");
d = -1;
Format.printf("-1 = |%X|\n", d);
Format.printf("100 = |%e|\n", 100.0); // 2000-06-09
Format.printf("1/0 = |%f|\n", 1.0 / 0.0);
Format.printf("-1/0 = |%e|\n", -1.0 / 0.0);
Format.printf("0/0 = |%g|\n", 0.0 / 0.0);
}
private static String repeat(char c, int n)
{
if (n <= 0) return "";
StringBuffer s = new StringBuffer(n);
for (int i = 0; i < n; i++) s.append(c);
return s.toString();
}
private static String convert(long x, int n, int m, String d)
{
if (x == 0) return "0";
String r = "";
while (x != 0)
{
r = d.charAt((int)(x & m)) + r;
x = x >>> n;
}
return r;
}
private String pad(String r)
{
String p = repeat(' ', width - r.length());
if (leftAlign) return pre + r + p + post;
else return pre + p + r + post;
}
private String sign(int s, String r)
{
String p = "";
if (s < 0) p = "-";
else if (s > 0)
{
if (showPlus) p = "+";
else if (showSpace) p = " ";
}
else
{
if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0') p = "0";
else if (fmt == 'x' && alternate) p = "0x";
else if (fmt == 'X' && alternate) p = "0X";
}
int w = 0;
if (leadingZeroes)
w = width;
else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o')
&& precision > 0) w = precision;
return p + repeat('0', w - p.length() - r.length()) + r;
}
private String fixedFormat(double d)
{
boolean removeTrailing
= (fmt == 'G' || fmt == 'g') && !alternate;
// remove trailing zeroes and decimal point
if (d > 0x7FFFFFFFFFFFFFFFL) return expFormat(d);
if (precision == 0)
return (long)(d + 0.5) + (removeTrailing ? "" : ".");
long whole = (long)d;
double fr = d - whole; // fractional part
if (fr >= 1 || fr < 0) return expFormat(d);
double factor = 1;
String leadingZeroes = "";
for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++)
{
factor *= 10;
leadingZeroes = leadingZeroes + "0";
}
long l = (long) (factor * fr + 0.5);
if (l >= factor) { l = 0; whole++; } // CSH 10-25-97
String z = leadingZeroes + l;
z = "." + z.substring(z.length() - precision, z.length());
if (removeTrailing)
{
int t = z.length() - 1;
while (t >= 0 && z.charAt(t) == '0') t--;
if (t >= 0 && z.charAt(t) == '.') t--;
z = z.substring(0, t + 1);
}
return whole + z;
}
private String expFormat(double d)
{
String f = "";
int e = 0;
double dd = d;
double factor = 1;
if (d != 0)
{
while (dd >= 10) { e++; factor /= 10; dd = dd / 10; } // 2000-06-09
while (dd < 1) { e--; factor *= 10; dd = dd * 10; }
}
if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision)
return fixedFormat(d);
d = d * factor;
f = f + fixedFormat(d);
if (fmt == 'e' || fmt == 'g')
f = f + "e";
else
f = f + "E";
String p = "000";
if (e >= 0)
{
f = f + "+";
p = p + e;
}
else
{
f = f + "-";
p = p + (-e);
}
return f + p.substring(p.length() - 3, p.length());
}
private int width;
private int precision;
private String pre;
private String post;
private boolean leadingZeroes;
private boolean showPlus;
private boolean alternate;
private boolean showSpace;
private boolean leftAlign;
private char fmt; // one of cdeEfgGiosxXos
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -