1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 /***
23 * A class for formatting numbers that follows printf conventions.
24 * Also implements C-like atoi and atof functions
25 * @version 1.1.3 Wed Jul 18 15:00:35 GMT 2001
26
27
28
29 * @author Cay Horstmann
30 */
31
32 package edu.sc.seis.TauP;
33
34
35
36
37
38
39
40
41
42
43
44 public class Format
45
46 { /***
47 * Formats the number following printf conventions.
48 * Main limitation: Can only handle one format parameter at a time
49 * Use multiple Format objects to format more than one number
50 * @param s the format string following printf conventions
51 * The string has a prefix, a format code and a suffix. The prefix and suffix
52 * become part of the formatted output. The format code directs the
53 * formatting of the (single) parameter to be formatted. The code has the
54 * following structure
55 * <ul>
56 * <li> a % (required)
57 * <li> a modifier (optional)
58 * <dl>
59 * <dt> + <dd> forces display of + for positive numbers
60 * <dt> 0 <dd> show leading zeroes
61 * <dt> - <dd> align left in the field
62 * <dt> space <dd> prepend a space in front of positive numbers
63 * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format.
64 * </dl>
65 * <li> an integer denoting field width (optional)
66 * <li> a period followed by an integer denoting precision (optional)
67 * <li> a format descriptor (required)
68 * <dl>
69 * <dt>f <dd> floating point number in fixed format
70 * <dt>e, E <dd> floating point number in exponential notation (scientific format). The E format results in an uppercase E for the exponent (1.14130E+003), the e format in a lowercase e.
71 * <dt>g, G <dd> floating point number in general format (fixed format for small numbers, exponential format for large numbers). Trailing zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format in a lowercase e.
72 * <dt>d, i <dd> integer in decimal
73 * <dt>x <dd> integer in hexadecimal
74 * <dt>o <dd> integer in octal
75 * <dt>s <dd> string
76 * <dt>c <dd> character
77 * </dl>
78 * </ul>
79 * @exception IllegalArgumentException if bad format
80 */
81
82 public Format(String s)
83 { width = 0;
84 precision = -1;
85 pre = "";
86 post = "";
87 leading_zeroes = false;
88 show_plus = false;
89 alternate = false;
90 show_space = false;
91 left_align = false;
92 fmt = ' ';
93
94 int state = 0;
95 int length = s.length();
96 int parse_state = 0;
97
98
99 int i = 0;
100
101 while (parse_state == 0)
102 { if (i >= length) parse_state = 5;
103 else if (s.charAt(i) == '%')
104 { if (i < length - 1)
105 { if (s.charAt(i + 1) == '%')
106 { pre = pre + '%';
107 i++;
108 }
109 else
110 parse_state = 1;
111 }
112 else throw new java.lang.IllegalArgumentException();
113 }
114 else
115 pre = pre + s.charAt(i);
116 i++;
117 }
118 while (parse_state == 1)
119 { if (i >= length) parse_state = 5;
120 else if (s.charAt(i) == ' ') show_space = true;
121 else if (s.charAt(i) == '-') left_align = true;
122 else if (s.charAt(i) == '+') show_plus = true;
123 else if (s.charAt(i) == '0') leading_zeroes = true;
124 else if (s.charAt(i) == '#') alternate = true;
125 else { parse_state = 2; i--; }
126 i++;
127 }
128 while (parse_state == 2)
129 { if (i >= length) parse_state = 5;
130 else if ('0' <= s.charAt(i) && s.charAt(i) <= '9')
131 { width = width * 10 + s.charAt(i) - '0';
132 i++;
133 }
134 else if (s.charAt(i) == '.')
135 { parse_state = 3;
136 precision = 0;
137 i++;
138 }
139 else
140 parse_state = 4;
141 }
142 while (parse_state == 3)
143 { if (i >= length) parse_state = 5;
144 else if ('0' <= s.charAt(i) && s.charAt(i) <= '9')
145 { precision = precision * 10 + s.charAt(i) - '0';
146 i++;
147 }
148 else
149 parse_state = 4;
150 }
151 if (parse_state == 4)
152 { if (i >= length) parse_state = 5;
153 else fmt = s.charAt(i);
154 i++;
155 }
156 if (i < length)
157 post = s.substring(i, length);
158 }
159
160 /***
161 * prints a formatted number following printf conventions
162 * @param s a PrintStream
163 * @param fmt the format string
164 * @param x the double to print
165 */
166
167 public static void print(java.io.PrintStream s, String fmt, double x)
168 { s.print(new Format(fmt).form(x));
169 }
170
171 /***
172 * prints a formatted number following printf conventions
173 * @param s a PrintStream
174 * @param fmt the format string
175 * @param x the long to print
176 */
177 public static void print(java.io.PrintStream s, String fmt, long x)
178 { s.print(new Format(fmt).form(x));
179 }
180
181 /***
182 * prints a formatted number following printf conventions
183 * @param s a PrintStream
184 * @param fmt the format string
185 * @param x the character to
186 */
187
188 public static void print(java.io.PrintStream s, String fmt, char x)
189 { s.print(new Format(fmt).form(x));
190 }
191
192 /***
193 * prints a formatted number following printf conventions
194 * @param s a PrintStream, fmt the format string
195 * @param x a string that represents the digits to print
196 */
197
198 public static void print(java.io.PrintStream s, String fmt, String x)
199 { s.print(new Format(fmt).form(x));
200 }
201
202 /***
203 * Converts a string of digits (decimal, octal or hex) to an integer
204 * @param s a string
205 * @return the numeric value of the prefix of s representing a base 10 integer
206 */
207
208 public static int atoi(String s)
209 { return (int)atol(s);
210 }
211
212 /***
213 * Converts a string of digits (decimal, octal or hex) to a long integer
214 * @param s a string
215 * @return the numeric value of the prefix of s representing a base 10 integer
216 */
217
218 public static long atol(String s)
219 { int i = 0;
220
221 while (i < s.length() && Character.isSpace(s.charAt(i))) i++;
222 if (i < s.length() && s.charAt(i) == '0')
223 { if (i + 1 < s.length() && (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X'))
224 return parseLong(s.substring(i + 2), 16);
225 else return parseLong(s, 8);
226 }
227 else return parseLong(s, 10);
228 }
229
230 private static long parseLong(String s, int base)
231 { int i = 0;
232 int sign = 1;
233 long r = 0;
234
235 while (i < s.length() && Character.isSpace(s.charAt(i))) i++;
236 if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
237 else if (i < s.length() && s.charAt(i) == '+') { i++; }
238 while (i < s.length())
239 { char ch = s.charAt(i);
240 if ('0' <= ch && ch < '0' + base)
241 r = r * base + ch - '0';
242 else if ('A' <= ch && ch < 'A' + base - 10)
243 r = r * base + ch - 'A' + 10 ;
244 else if ('a' <= ch && ch < 'a' + base - 10)
245 r = r * base + ch - 'a' + 10 ;
246 else
247 return r * sign;
248 i++;
249 }
250 return r * sign;
251 }
252
253 /***
254 * Converts a string of digits to an double
255 * @param s a string
256 */
257
258 public static double atof(String s)
259 { int i = 0;
260 int sign = 1;
261 double r = 0;
262 double f = 0;
263 double p = 1;
264 int state = 0;
265
266 while (i < s.length() && Character.isSpace(s.charAt(i))) i++;
267 if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; }
268 else if (i < s.length() && s.charAt(i) == '+') { i++; }
269 while (i < s.length())
270 { char ch = s.charAt(i);
271 if ('0' <= ch && ch <= '9')
272 { if (state == 0)
273 r = r * 10 + ch - '0';
274 else if (state == 1)
275 { p = p / 10;
276 r = r + p * (ch - '0');
277 }
278 }
279 else if (ch == '.')
280 { if (state == 0) state = 1;
281 else return sign * r;
282 }
283 else if (ch == 'e' || ch == 'E')
284 { long e = (int)parseLong(s.substring(i + 1), 10);
285 return sign * r * Math.pow(10, e);
286 }
287 else return sign * r;
288 i++;
289 }
290 return sign * r;
291 }
292
293 /***
294 * Formats a double into a string (like sprintf in C)
295 * @param x the number to format
296 * @return the formatted string
297 * @exception IllegalArgumentException if bad argument
298 */
299
300 public String form(double x)
301 { String r;
302 if (precision < 0) precision = 6;
303 int s = 1;
304 if (x < 0) { x = -x; s = -1; }
305 if (fmt == 'f')
306 r = fixed_format(x);
307 else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G')
308 r = exp_format(x);
309 else throw new java.lang.IllegalArgumentException();
310
311 return pad(sign(s, r));
312 }
313
314 /***
315 * Formats a long integer into a string (like sprintf in C)
316 * @param x the number to format
317 * @return the formatted string
318 */
319
320 public String form(long x)
321 { String r;
322 int s = 0;
323 if (fmt == 'd' || fmt == 'i')
324 { s = 1;
325 if (x < 0) { x = -x; s = -1; }
326 r = "" + x;
327 }
328 else if (fmt == 'o')
329 r = convert(x, 3, 7, "01234567");
330 else if (fmt == 'x')
331 r = convert(x, 4, 15, "0123456789abcdef");
332 else if (fmt == 'X')
333 r = convert(x, 4, 15, "0123456789ABCDEF");
334 else throw new java.lang.IllegalArgumentException();
335
336 return pad(sign(s, r));
337 }
338
339 /***
340 * Formats a character into a string (like sprintf in C)
341 * @param x the value to format
342 * @return the formatted string
343 */
344
345 public String form(char c)
346 { if (fmt != 'c')
347 throw new java.lang.IllegalArgumentException();
348
349 String r = "" + c;
350 return pad(r);
351 }
352
353 /***
354 * Formats a string into a larger string (like sprintf in C)
355 * @param x the value to format
356 * @return the formatted string
357 */
358
359 public String form(String s)
360 { if (fmt != 's')
361 throw new java.lang.IllegalArgumentException();
362 if (precision >= 0) s = s.substring(0, precision);
363 return pad(s);
364 }
365
366
367 /***
368 * a test stub for the format class
369 */
370
371 public static void main(String[] a)
372 { double x = 1.23456789012;
373 double y = 123;
374 double z = 1.2345e30;
375 double w = 1.02;
376 double u = 1.234e-5;
377 int d = 0xCAFE;
378 Format.print(System.out, "x = |%f|\n", x);
379 Format.print(System.out, "u = |%20f|\n", u);
380 Format.print(System.out, "x = |% .5f|\n", x);
381 Format.print(System.out, "w = |%20.5f|\n", w);
382 Format.print(System.out, "x = |%020.5f|\n", x);
383 Format.print(System.out, "x = |%+20.5f|\n", x);
384 Format.print(System.out, "x = |%+020.5f|\n", x);
385 Format.print(System.out, "x = |% 020.5f|\n", x);
386 Format.print(System.out, "y = |%#+20.5f|\n", y);
387 Format.print(System.out, "y = |%-+20.5f|\n", y);
388 Format.print(System.out, "z = |%20.5f|\n", z);
389
390 Format.print(System.out, "x = |%e|\n", x);
391 Format.print(System.out, "u = |%20e|\n", u);
392 Format.print(System.out, "x = |% .5e|\n", x);
393 Format.print(System.out, "w = |%20.5e|\n", w);
394 Format.print(System.out, "x = |%020.5e|\n", x);
395 Format.print(System.out, "x = |%+20.5e|\n", x);
396 Format.print(System.out, "x = |%+020.5e|\n", x);
397 Format.print(System.out, "x = |% 020.5e|\n", x);
398 Format.print(System.out, "y = |%#+20.5e|\n", y);
399 Format.print(System.out, "y = |%-+20.5e|\n", y);
400
401 Format.print(System.out, "x = |%g|\n", x);
402 Format.print(System.out, "z = |%g|\n", z);
403 Format.print(System.out, "w = |%g|\n", w);
404 Format.print(System.out, "u = |%g|\n", u);
405 Format.print(System.out, "y = |%.2g|\n", y);
406 Format.print(System.out, "y = |%#.2g|\n", y);
407
408 Format.print(System.out, "d = |%d|\n", d);
409 Format.print(System.out, "d = |%20d|\n", d);
410 Format.print(System.out, "d = |%020d|\n", d);
411 Format.print(System.out, "d = |%+20d|\n", d);
412 Format.print(System.out, "d = |% 020d|\n", d);
413 Format.print(System.out, "d = |%-20d|\n", d);
414 Format.print(System.out, "d = |%20.8d|\n", d);
415 Format.print(System.out, "d = |%x|\n", d);
416 Format.print(System.out, "d = |%20X|\n", d);
417 Format.print(System.out, "d = |%#20x|\n", d);
418 Format.print(System.out, "d = |%020X|\n", d);
419 Format.print(System.out, "d = |%20.8x|\n", d);
420 Format.print(System.out, "d = |%o|\n", d);
421 Format.print(System.out, "d = |%020o|\n", d);
422 Format.print(System.out, "d = |%#20o|\n", d);
423 Format.print(System.out, "d = |%#020o|\n", d);
424 Format.print(System.out, "d = |%20.12o|\n", d);
425
426 Format.print(System.out, "s = |%-20s|\n", "Hello");
427 Format.print(System.out, "s = |%-20c|\n", '!');
428 }
429
430
431 private static String repeat(char c, int n)
432 { if (n <= 0) return "";
433 StringBuffer s = new StringBuffer(n);
434 for (int i = 0; i < n; i++) s.append(c);
435 return s.toString();
436 }
437
438 private static String convert(long x, int n, int m, String d)
439 { if (x == 0) return "0";
440 String r = "";
441 while (x != 0)
442 { r = d.charAt((int)(x & m)) + r;
443 x = x >>> n;
444 }
445 return r;
446 }
447
448 private String pad(String r)
449 { String p = repeat(' ', width - r.length());
450 if (left_align) return pre + r + p + post;
451 else return pre + p + r + post;
452 }
453
454 private String sign(int s, String r)
455 { String p = "";
456 if (s < 0) p = "-";
457 else if (s > 0)
458 { if (show_plus) p = "+";
459 else if (show_space) p = " ";
460 }
461 else
462 { if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0') p = "0";
463 else if (fmt == 'x' && alternate) p = "0x";
464 else if (fmt == 'X' && alternate) p = "0X";
465 }
466 int w = 0;
467 if (leading_zeroes)
468 w = width;
469 else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o')
470 && precision > 0) w = precision;
471
472 return p + repeat('0', w - p.length() - r.length()) + r;
473 }
474
475
476 private String fixed_format(double d)
477 { String f = "";
478
479 if (d > 0x7FFFFFFFFFFFFFFFL) return exp_format(d);
480
481 if (precision > 0) {
482 d = Math.rint(d * Math.pow(10, precision))/Math.pow(10, precision);
483 }
484 long l = (long)(precision == 0 ? d + 0.5 : d);
485 f = f + l;
486
487 double fr = d - l;
488 if (fr >= 1 || fr < 0) return exp_format(d);
489
490 return f + frac_part(fr);
491 }
492
493 private String frac_part(double fr)
494
495 { String z = "";
496 if (precision > 0)
497 { double factor = 1;
498 String leading_zeroes = "";
499 for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++)
500 { factor *= 10;
501 leading_zeroes = leading_zeroes + "0";
502 }
503 long l = (long) (factor * fr + 0.5);
504
505 z = leading_zeroes + l;
506 z = z.substring(z.length() - precision, z.length());
507 }
508
509
510 if (precision > 0 || alternate) z = "." + z;
511 if ((fmt == 'G' || fmt == 'g') && !alternate)
512
513 { int t = z.length() - 1;
514 while (t >= 0 && z.charAt(t) == '0') t--;
515 if (t >= 0 && z.charAt(t) == '.') t--;
516 z = z.substring(0, t + 1);
517 }
518 return z;
519 }
520
521 private String exp_format(double d)
522 { String f = "";
523 int e = 0;
524 double dd = d;
525 double factor = 1;
526 while (dd > 10) { e++; factor /= 10; dd = dd / 10; }
527 while (dd < 1) { e--; factor *= 10; dd = dd * 10; }
528 if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision)
529 return fixed_format(d);
530
531 d = d * factor;
532 f = f + fixed_format(d);
533
534 if (fmt == 'e' || fmt == 'g')
535 f = f + "e";
536 else
537 f = f + "E";
538
539 String p = "000";
540 if (e >= 0)
541 { f = f + "+";
542 p = p + e;
543 }
544 else
545 { f = f + "-";
546 p = p + (-e);
547 }
548
549 return f + p.substring(p.length() - 3, p.length());
550 }
551
552 private int width;
553 private int precision;
554 private String pre;
555 private String post;
556 private boolean leading_zeroes;
557 private boolean show_plus;
558 private boolean alternate;
559 private boolean show_space;
560 private boolean left_align;
561 private char fmt;
562 }