📄 format.java
字号:
/**
* Formats a long integer into a string (like sprintf in C)
*
* @param x the number to format
*
* @return the formatted string
*/
public String form(long x) {
String r;
int s = 0;
if (fmt == 'd') {
if (x < 0) {
r = ("" + x).substring(1);
s = -1;
} else {
r = "" + x;
s = 1;
}
} else if (fmt == 'i') {
int xx = (int) x;
if (xx < 0) {
r = ("" + xx).substring(1);
s = -1;
} else {
r = "" + xx;
s = 1;
}
} else if (fmt == 'u') {
long xl = x & 0x00000000FFFFFFFFL;
r = "" + xl;
s = 1;
} else if (fmt == 'o') {
r = convert(x, 3, "01234567");
} else if (fmt == 'x') {
r = convert(x, 4, "0123456789abcdef");
} else if (fmt == 'X') {
r = convert(x, 4, "0123456789ABCDEF");
} else {
throw new java.lang.IllegalArgumentException();
}
return pad(sign(s, r));
}
public String form(Long x) {
return form(x.longValue());
}
/**
* Formats an integer into a string (like sprintf in C)
*
* @param x the number to format
*
* @return the formatted string
*/
public String form(int 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 {
long xl = x & 0x00000000FFFFFFFFL;
if (fmt == 'u') {
r = "" + xl;
s = 1;
} else if (fmt == 'o') {
r = convert(xl, 3, "01234567");
} else if (fmt == 'x') {
r = convert(xl, 4, "0123456789abcdef");
} else if (fmt == 'X') {
r = convert(xl, 4, "0123456789ABCDEF");
} else {
throw new java.lang.IllegalArgumentException();
}
}
return pad(sign(s, r));
}
public String form(Integer x) {
return form(x.intValue());
}
/**
* Formats a string into a larger string (like sprintf in C)
*
* @param s the value to format
*
* @return the formatted string
*/
public String form(String s) {
if (fmt != 's') {
throw new java.lang.IllegalArgumentException();
}
if (precision >= 0 && precision < s.length()) {
s = s.substring(0, precision);
}
return pad(s);
}
// ---------------------------------------------------------------- misc conversion
/**
* Converts a string of digits to an double
*
* @param s a string
*
* @return double converted from String
*/
public static double atof(String s) {
int i = 0;
int sign = 1;
double r = 0; // integer part
double f = 0; // fractional part
double p = 1; // exponent of fractional part
int state = 0; // 0 = int part, 1 = frac part
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
i++;
}
if (i < s.length() && s.charAt(i) == '-') {
sign = -1;
i++;
} else if (i < s.length() && s.charAt(i) == '+') {
i++;
}
while (i < s.length()) {
char ch = s.charAt(i);
if ('0' <= ch && ch <= '9') {
if (state == 0) {
r = r * 10 + ch - '0';
} else if (state == 1) {
p = p / 10;
r = r + p * (ch - '0');
}
} else if (ch == '.') {
if (state == 0) {
state = 1;
} else {
return sign * r;
}
} else if (ch == 'e' || ch == 'E') {
long e = (int) parseLong(s.substring(i + 1), 10);
return sign * r * Math.pow(10, e);
} else {
return sign * r;
}
i++;
}
return sign * r;
}
/**
* Converts a string of digits (decimal, octal or hex) to an integer
*
* @param s a string
*
* @return the numeric value of the prefix of s representing a
* base 10 integer
*/
public static int atoi(String s) {
return (int) atol(s);
}
/**
* Converts a string of digits (decimal, octal or hex) to a long integer
*
* @param s a string
*
* @return the numeric value of the prefix of s representing a
* base 10 integer
*/
public static long atol(String s) {
int i = 0;
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
i++;
}
if (i < s.length() && s.charAt(i) == '0') {
if (i + 1 < s.length() &&
(s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X')) {
return parseLong(s.substring(i + 2), 16);
} else {
return parseLong(s, 8);
}
} else {
return parseLong(s, 10);
}
}
/**
* Converts number to string
*
* @param x value to convert
* @param n conversion base
* @param d string with characters for conversion.
*
* @return converted number as string
*/
public static String convert(long x, int n, String d) {
if (x == 0) {
return "0";
}
String r = "";
int m = 1 << n;
m--;
while (x != 0) {
r = d.charAt((int) (x & m)) + r;
x = x >>> n;
}
return r;
}
// ---------------------------------------------------------------- sprintf
/**
* prints a formatted number following printf conventions
*
* @param fmt the format string
* @param x the character to
*
* @return formated string
*/
public static String sprintf(String fmt, char x) {
return new Format(fmt).form(x);
}
public static String sprintf(String fmt, Character x) {
return new Format(fmt).form(x);
}
/**
* prints a formatted number following printf conventions
*
* @param fmt the format string
* @param x the double to print
*
* @return formated string
*/
public static String sprintf(String fmt, double x) {
return new Format(fmt).form(x);
}
public static String sprintf(String fmt, Double x) {
return new Format(fmt).form(x);
}
public static String sprintf(String fmt, Float x) {
return new Format(fmt).form(x);
}
/**
* prints a formatted number following printf conventions
*
* @param fmt the format string
* @param x the long to print
*
* @return formated string
*/
public static String sprintf(String fmt, long x) {
return new Format(fmt).form(x);
}
public static String sprintf(String fmt, Long x) {
return new Format(fmt).form(x);
}
/**
* prints a formatted number following printf conventions
*
* @param fmt the format string
* @param x the int to print
*
* @return formated string
*/
public static String sprintf(String fmt, int x) {
return new Format(fmt).form(x);
}
public static String sprintf(String fmt, Integer x) {
return new Format(fmt).form(x);
}
/**
* prints a formatted number following printf conventions
*
* @param fmt
* @param x a string that represents the digits to print
*/
public static String sprintf(String fmt, String x) {
return new Format(fmt).form(x);
}
// ---------------------------------------------------------------- array sprintf
/**
* Splits input string on '%' and returns an array of substrings that can be
* used for multiple scanf. Each substring has a one-char prefix that can
* take one of two values:
* '+' wich indicates that substring can be simply added to the resulting string
* ' ' wich indicates that substring has to be additionally processed.
*
* @param s Input string
*
* @return splited input string
*/
private static String[] split(String s) {
ArrayList list = new ArrayList();
int lasti = 0;
int i = s.indexOf("%");
if (i == -1) {
return new String[] {s};
}
if (i > 0) {
list.add("+" + s.substring(0, i));
lasti = i;
i++;
i = s.indexOf("%", i);
} else if (i == 0) {
i = s.indexOf("%", i + 1);
}
while (i != -1) {
String ss = s.substring(lasti, i);
if (ss.equals("%")) {
lasti = i; i++;
i = s.indexOf("%", i);
if (i != -1) {
ss = s.substring(lasti, i);
} else {
ss = s.substring(lasti);
}
list.add("+" + ss);
} else {
list.add(" " + ss);
}
if (i == -1) {
lasti = i;
break;
}
lasti = i;
i++;
i = s.indexOf("%", i);
}
if (lasti != -1) {
list.add(" " + s.substring(lasti));
}
String[] ret = new String[list.size()];
for (i = 0; i < list.size(); i++) {
ret[i] = (String) list.get(i);
}
return ret;
}
/**
* Sprintf multiple strings.
*
* @param s
* @param params
*
* @return formated string
*/
public static String sprintf(String s, Object[] params) {
if ((s == null) || (params == null)) {
return s;
}
StringBuffer result = new StringBuffer("");
String[] ss = split(s);
int p = 0;
for (int i = 0; i < ss.length; i++) {
char c = ss[i].charAt(0);
String t = ss[i].substring(1);
if (c == '+') {
result.append(t);
} else {
Object param = params[p];
if (param instanceof Integer) {
result.append(new Format(t).form((Integer) param));
} else if (param instanceof Long) {
result.append(new Format(t).form((Long) param));
} else if (param instanceof Character) {
result.append(new Format(t).form((Character) param));
} else if (param instanceof Float) {
result.append(new Format(t).form((Float) param));
} else if (param instanceof Double) {
result.append(new Format(t).form((Double) param));
} else {
result.append(new Format(t).form(param.toString()));
}
p++;
}
}
return result.toString();
}
public static String sprintf(String s, String[] params) {
return sprintf(s, (Object[]) params);
}
public static String sprintf(String s, Integer[] params) {
return sprintf(s, (Object[]) params);
}
public static String sprintf(String s, Long[] params) {
return sprintf(s, (Object[]) params);
}
public static String sprintf(String s, Double[] params) {
return sprintf(s, (Object[]) params);
}
public static String sprintf(String s, Float[] params) {
return sprintf(s, (Object[]) params);
}
public static String sprintf(String s, Character[] params) {
return sprintf(s, (Object[]) params);
}
// ---------------------------------------------------------------- primitives array sprintf
public static String sprintf(String s, int[] params) {
if ((s == null) || (params == null)) {
return s;
}
StringBuffer result = new StringBuffer("");
String[] ss = split(s);
int p = 0;
for (int i = 0; i < ss.length; i++) {
char c = ss[i].charAt(0);
String t = ss[i].substring(1);
if (c == '+') {
result.append(t);
} else {
int param = params[p];
result.append(new Format(t).form(param));
p++;
}
}
return result.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -