- if (fmt == 'f')\r
- r = fixed_format(x);\r
- else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G')\r
- r = exp_format(x);\r
- else\r
- throw new java.lang.IllegalArgumentException();\r
-\r
- return pad(sign(s, r));\r
- }\r
-\r
- /**\r
- * Formats a long integer into a string (like sprintf in C)\r
- * @param x the number to format\r
- * @return the formatted string \r
- */\r
-\r
- public String form(long x) {\r
- String r;\r
- int s = 0;\r
- if (fmt == 'd' || fmt == 'i') {\r
- if (x < 0) {\r
- r = ("" + x).substring(1);\r
- s = -1;\r
- } else {\r
- r = "" + x;\r
- s = 1;\r
- }\r
- } else if (fmt == 'o')\r
- r = convert(x, 3, 7, "01234567");\r
- else if (fmt == 'x')\r
- r = convert(x, 4, 15, "0123456789abcdef");\r
- else if (fmt == 'X')\r
- r = convert(x, 4, 15, "0123456789ABCDEF");\r
- else\r
- throw new java.lang.IllegalArgumentException();\r
-\r
- return pad(sign(s, r));\r
- }\r
-\r
- /**\r
- * Formats a character into a string (like sprintf in C)\r
- * @param x the value to format\r
- * @return the formatted string \r
- */\r
-\r
- public String form(char c) {\r
- if (fmt != 'c')\r
- throw new java.lang.IllegalArgumentException();\r
-\r
- String r = "" + c;\r
- return pad(r);\r
- }\r
-\r
- /**\r
- * Formats a string into a larger string (like sprintf in C)\r
- * @param x the value to format\r
- * @return the formatted string \r
- */\r
-\r
- public String form(String s) {\r
- if (fmt != 's')\r
- throw new java.lang.IllegalArgumentException();\r
- if (precision >= 0)\r
- s = s.substring(0, precision);\r
- return pad(s);\r
- }\r
-\r
-\r
- /**\r
- * a test stub for the format class\r
- */\r
-\r
- public static void main(String[] a) {\r
- double x = 1.23456789012;\r
- double y = 123;\r
- double z = 1.2345e30;\r
- double w = 1.02;\r
- double u = 1.234e-5;\r
- int d = 0xCAFE;\r
- Format.print(System.out, "x = |%f|\n", x);\r
- Format.print(System.out, "u = |%20f|\n", u);\r
- Format.print(System.out, "x = |% .5f|\n", x);\r
- Format.print(System.out, "w = |%20.5f|\n", w);\r
- Format.print(System.out, "x = |%020.5f|\n", x);\r
- Format.print(System.out, "x = |%+20.5f|\n", x);\r
- Format.print(System.out, "x = |%+020.5f|\n", x);\r
- Format.print(System.out, "x = |% 020.5f|\n", x);\r
- Format.print(System.out, "y = |%#+20.5f|\n", y);\r
- Format.print(System.out, "y = |%-+20.5f|\n", y);\r
- Format.print(System.out, "z = |%20.5f|\n", z);\r
-\r
- Format.print(System.out, "x = |%e|\n", x);\r
- Format.print(System.out, "u = |%20e|\n", u);\r
- Format.print(System.out, "x = |% .5e|\n", x);\r
- Format.print(System.out, "w = |%20.5e|\n", w);\r
- Format.print(System.out, "x = |%020.5e|\n", x);\r
- Format.print(System.out, "x = |%+20.5e|\n", x);\r
- Format.print(System.out, "x = |%+020.5e|\n", x);\r
- Format.print(System.out, "x = |% 020.5e|\n", x);\r
- Format.print(System.out, "y = |%#+20.5e|\n", y);\r
- Format.print(System.out, "y = |%-+20.5e|\n", y);\r
-\r
- Format.print(System.out, "x = |%g|\n", x);\r
- Format.print(System.out, "z = |%g|\n", z);\r
- Format.print(System.out, "w = |%g|\n", w);\r
- Format.print(System.out, "u = |%g|\n", u);\r
- Format.print(System.out, "y = |%.2g|\n", y);\r
- Format.print(System.out, "y = |%#.2g|\n", y);\r
-\r
- Format.print(System.out, "d = |%d|\n", d);\r
- Format.print(System.out, "d = |%20d|\n", d);\r
- Format.print(System.out, "d = |%020d|\n", d);\r
- Format.print(System.out, "d = |%+20d|\n", d);\r
- Format.print(System.out, "d = |% 020d|\n", d);\r
- Format.print(System.out, "d = |%-20d|\n", d);\r
- Format.print(System.out, "d = |%20.8d|\n", d);\r
- Format.print(System.out, "d = |%x|\n", d);\r
- Format.print(System.out, "d = |%20X|\n", d);\r
- Format.print(System.out, "d = |%#20x|\n", d);\r
- Format.print(System.out, "d = |%020X|\n", d);\r
- Format.print(System.out, "d = |%20.8x|\n", d);\r
- Format.print(System.out, "d = |%o|\n", d);\r
- Format.print(System.out, "d = |%020o|\n", d);\r
- Format.print(System.out, "d = |%#20o|\n", d);\r
- Format.print(System.out, "d = |%#020o|\n", d);\r
- Format.print(System.out, "d = |%20.12o|\n", d);\r
-\r
- Format.print(System.out, "s = |%-20s|\n", "Hello");\r
- Format.print(System.out, "s = |%-20c|\n", '!');\r
-\r
- // regression test to confirm fix of reported bugs\r
-\r
- Format.print(System.out, "|%i|\n", Long.MIN_VALUE);\r
-\r
- Format.print(System.out, "|%6.2e|\n", 0.0);\r
- Format.print(System.out, "|%6.2g|\n", 0.0);\r
-\r
- Format.print(System.out, "|%6.2f|\n", 9.99);\r
- Format.print(System.out, "|%6.2f|\n", 9.999);\r
-\r
- Format.print(System.out, "|%6.0f|\n", 9.999);\r
- }\r
-\r
- private static String repeat(char c, int n) {\r
- if (n <= 0)\r
- return "";\r
- StringBuffer s = new StringBuffer(n);\r
- for (int i = 0; i < n; i++)\r
- s.append(c);\r
- return s.toString();\r
- }\r
-\r
- private static String convert(long x, int n, int m, String d) {\r
- if (x == 0)\r
- return "0";\r
- String r = "";\r
- while (x != 0) {\r
- r = d.charAt((int)(x & m)) + r;\r
- x = x >>> n;\r