1 | /* |
---|
2 | * $Id: Format.java,v 1.4 2002/03/04 16:37:57 dwd Exp $ |
---|
3 | * |
---|
4 | * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM) |
---|
5 | * Published By SunSoft Press/Prentice-Hall |
---|
6 | * Copyright (C) 1996 Sun Microsystems Inc. |
---|
7 | * All Rights Reserved. ISBN 0-13-596891-7 |
---|
8 | * |
---|
9 | * Permission to use, copy, modify, and distribute this |
---|
10 | * software and its documentation for NON-COMMERCIAL purposes |
---|
11 | * and without fee is hereby granted provided that this |
---|
12 | * copyright notice appears in all copies. |
---|
13 | * |
---|
14 | * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR |
---|
15 | * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER |
---|
16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
---|
18 | * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS |
---|
19 | * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED |
---|
20 | * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING |
---|
21 | * THIS SOFTWARE OR ITS DERIVATIVES. |
---|
22 | */ |
---|
23 | |
---|
24 | package gov.noaa.pmel.sgt; |
---|
25 | import java.io.*; |
---|
26 | /** |
---|
27 | * A class for formatting numbers that follows printf conventions. |
---|
28 | * Also implements C-like atoi and atof functions |
---|
29 | * |
---|
30 | * @version 1.01 15 Feb 1996 |
---|
31 | * @since 1.0 |
---|
32 | * @author Cay Horstmann |
---|
33 | */ |
---|
34 | public class Format { |
---|
35 | private int width; |
---|
36 | private int precision; |
---|
37 | private String pre; |
---|
38 | private String post; |
---|
39 | private boolean leading_zeroes; |
---|
40 | private boolean show_plus; |
---|
41 | private boolean alternate; |
---|
42 | private boolean show_space; |
---|
43 | private boolean left_align; |
---|
44 | private char fmt; |
---|
45 | private static long parseLong(String s,int base) { |
---|
46 | int i = 0; int sign = 1; |
---|
47 | long r = 0; |
---|
48 | |
---|
49 | while (i < s.length() && Character.isWhitespace(s.charAt(i))) i++; |
---|
50 | if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; } |
---|
51 | else if (i < s.length() && s.charAt(i) == '+') { i++; } |
---|
52 | while (i < s.length()) |
---|
53 | { char ch = s.charAt(i); |
---|
54 | if ('0' <= ch && ch < '0' + base) |
---|
55 | r = r * base + ch - '0'; |
---|
56 | else if ('A' <= ch && ch < 'A' + base - 10) |
---|
57 | r = r * base + ch - 'A' + 10 ; |
---|
58 | else if ('a' <= ch && ch < 'a' + base - 10) |
---|
59 | r = r * base + ch - 'a' + 10 ; |
---|
60 | else |
---|
61 | return r * sign; |
---|
62 | i++; |
---|
63 | } |
---|
64 | return r * sign; |
---|
65 | } |
---|
66 | /** |
---|
67 | * a test stub for the format class |
---|
68 | */ |
---|
69 | /*public static void main(String[] a) |
---|
70 | { double x = 1.23456789012; |
---|
71 | double xx= 1.0000006; |
---|
72 | double y = 123; |
---|
73 | double z = 1.2345e30; |
---|
74 | double w = 1.02; |
---|
75 | double u = 1.234e-5; |
---|
76 | int d = 0xCAFE; |
---|
77 | Format.print(System.out, "xx = |%.6f|\n", xx); |
---|
78 | Format.print(System.out, "x = |%f|\n", x); |
---|
79 | Format.print(System.out, "u = |%20f|\n", u); |
---|
80 | Format.print(System.out, "x = |% .5f|\n", x); |
---|
81 | Format.print(System.out, "w = |%20.5f|\n", w); |
---|
82 | Format.print(System.out, "x = |%020.5f|\n", x); |
---|
83 | Format.print(System.out, "x = |%+20.5f|\n", x); |
---|
84 | Format.print(System.out, "x = |%+020.5f|\n", x); |
---|
85 | Format.print(System.out, "x = |% 020.5f|\n", x); |
---|
86 | Format.print(System.out, "y = |%#+20.5f|\n", y); |
---|
87 | Format.print(System.out, "y = |%-+20.5f|\n", y); |
---|
88 | Format.print(System.out, "z = |%20.5f|\n", z); |
---|
89 | |
---|
90 | Format.print(System.out, "x = |%e|\n", x); |
---|
91 | Format.print(System.out, "u = |%20e|\n", u); |
---|
92 | Format.print(System.out, "x = |% .5e|\n", x); |
---|
93 | Format.print(System.out, "w = |%20.5e|\n", w); |
---|
94 | Format.print(System.out, "x = |%020.5e|\n", x); |
---|
95 | Format.print(System.out, "x = |%+20.5e|\n", x); |
---|
96 | Format.print(System.out, "x = |%+020.5e|\n", x); |
---|
97 | Format.print(System.out, "x = |% 020.5e|\n", x); |
---|
98 | Format.print(System.out, "y = |%#+20.5e|\n", y); |
---|
99 | Format.print(System.out, "y = |%-+20.5e|\n", y); |
---|
100 | |
---|
101 | Format.print(System.out, "x = |%g|\n", x); |
---|
102 | Format.print(System.out, "z = |%g|\n", z); |
---|
103 | Format.print(System.out, "w = |%g|\n", w); |
---|
104 | Format.print(System.out, "u = |%g|\n", u); |
---|
105 | Format.print(System.out, "y = |%.2g|\n", y); |
---|
106 | Format.print(System.out, "y = |%#.2g|\n", y); |
---|
107 | |
---|
108 | Format.print(System.out, "d = |%d|\n", d); |
---|
109 | Format.print(System.out, "d = |%20d|\n", d); |
---|
110 | Format.print(System.out, "d = |%020d|\n", d); |
---|
111 | Format.print(System.out, "d = |%+20d|\n", d); |
---|
112 | Format.print(System.out, "d = |% 020d|\n", d); |
---|
113 | Format.print(System.out, "d = |%-20d|\n", d); |
---|
114 | Format.print(System.out, "d = |%20.8d|\n", d); |
---|
115 | Format.print(System.out, "d = |%x|\n", d); |
---|
116 | Format.print(System.out, "d = |%20X|\n", d); |
---|
117 | Format.print(System.out, "d = |%#20x|\n", d); |
---|
118 | Format.print(System.out, "d = |%020X|\n", d); |
---|
119 | Format.print(System.out, "d = |%20.8x|\n", d); |
---|
120 | Format.print(System.out, "d = |%o|\n", d); |
---|
121 | Format.print(System.out, "d = |%020o|\n", d); |
---|
122 | Format.print(System.out, "d = |%#20o|\n", d); |
---|
123 | Format.print(System.out, "d = |%#020o|\n", d); |
---|
124 | Format.print(System.out, "d = |%20.12o|\n", d); |
---|
125 | |
---|
126 | Format.print(System.out, "s = |%-20s|\n", "Hello"); |
---|
127 | Format.print(System.out, "s = |%-20c|\n", '!'); |
---|
128 | }*/ |
---|
129 | |
---|
130 | private static String repeat(char c,int n) { |
---|
131 | if (n <= 0) return ""; StringBuffer s = new StringBuffer(n); |
---|
132 | for (int i = 0; i < n; i++) s.append(c); |
---|
133 | return s.toString(); |
---|
134 | } |
---|
135 | private static String convert(long x,int n,int m,String d) { |
---|
136 | if (x == 0) return "0"; String r = ""; |
---|
137 | while (x != 0) |
---|
138 | { r = d.charAt((int)(x & m)) + r; |
---|
139 | x = x >>> n; |
---|
140 | } |
---|
141 | return r; |
---|
142 | } |
---|
143 | private String pad(String r) { |
---|
144 | String p = repeat(' ', width - r.length()); if (left_align) return pre + r + p + post; |
---|
145 | else return pre + p + r + post; |
---|
146 | } |
---|
147 | private String sign(int s,String r) { |
---|
148 | String p = ""; if (s < 0) p = "-"; |
---|
149 | else if (s > 0) |
---|
150 | { if (show_plus) p = "+"; |
---|
151 | else if (show_space) p = " "; |
---|
152 | } |
---|
153 | else |
---|
154 | { if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0') p = "0"; |
---|
155 | else if (fmt == 'x' && alternate) p = "0x"; |
---|
156 | else if (fmt == 'X' && alternate) p = "0X"; |
---|
157 | } |
---|
158 | int w = 0; |
---|
159 | if (leading_zeroes) |
---|
160 | w = width; |
---|
161 | else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o') |
---|
162 | && precision > 0) w = precision; |
---|
163 | |
---|
164 | return p + repeat('0', w - p.length() - r.length()) + r; |
---|
165 | } |
---|
166 | private String fixed_format(double d) { |
---|
167 | String f = ""; |
---|
168 | if (d > Long.MAX_VALUE) return exp_format(d); |
---|
169 | //here is the first change to the code (04/03/02) |
---|
170 | long l=(long) (d+=0.5*Math.pow(10D,-precision)); |
---|
171 | //long l = (long)(precision == 0 ? d + 0.5 : d); |
---|
172 | f =f + l; |
---|
173 | double fr = d - l; // fractional part |
---|
174 | if (fr >= 1 || fr < 0 && precision != 0) return exp_format(d); |
---|
175 | |
---|
176 | return f + frac_part(fr); |
---|
177 | } |
---|
178 | // precondition: 0 <= fr < 1 |
---|
179 | private String frac_part(double fr) { |
---|
180 | String z = ""; |
---|
181 | if (precision > 0){ |
---|
182 | double factor = 1; |
---|
183 | String leading_zeroes = ""; |
---|
184 | for (int i = 1; i <= precision && factor <= Double.MAX_VALUE; i++) { |
---|
185 | factor *= 10; |
---|
186 | leading_zeroes = leading_zeroes + "0"; |
---|
187 | } |
---|
188 | //here is the second change to the code (04/03/02) |
---|
189 | long l = (long) (factor * fr ); |
---|
190 | //long l = (long) (factor * fr + 0.5); |
---|
191 | z = leading_zeroes + l; |
---|
192 | z = z.substring(z.length() - precision, z.length()); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | if (precision > 0 || alternate) z = "." + z; |
---|
197 | if ((fmt == 'G' || fmt == 'g') && !alternate) |
---|
198 | // remove trailing zeroes and decimal point |
---|
199 | { int t = z.length() - 1; |
---|
200 | while (t >= 0 && z.charAt(t) == '0') t--; |
---|
201 | if (t >= 0 && z.charAt(t) == '.') t--; |
---|
202 | z = z.substring(0, t + 1); |
---|
203 | } |
---|
204 | return z; |
---|
205 | } |
---|
206 | private String exp_format(double d) { |
---|
207 | String f = ""; int e = 0; |
---|
208 | double dd = d; |
---|
209 | double factor = 1; |
---|
210 | if (dd == 0) return "0"; |
---|
211 | while (dd > 10) { e++; factor /= 10; dd = dd / 10; } |
---|
212 | while (dd < 1) { e--; factor *= 10; dd = dd * 10; } |
---|
213 | if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision) |
---|
214 | return fixed_format(d); |
---|
215 | |
---|
216 | d = d * factor; |
---|
217 | f = f + fixed_format(d); |
---|
218 | |
---|
219 | if (fmt == 'e' || fmt == 'g') |
---|
220 | f = f + "e"; |
---|
221 | else |
---|
222 | f = f + "E"; |
---|
223 | |
---|
224 | String p = "000"; |
---|
225 | if (e >= 0) |
---|
226 | { f = f + "+"; |
---|
227 | p = p + e; |
---|
228 | } |
---|
229 | else |
---|
230 | { f = f + "-"; |
---|
231 | p = p + (-e); |
---|
232 | } |
---|
233 | |
---|
234 | return f + p.substring(p.length() - 3, p.length()); |
---|
235 | } |
---|
236 | /** |
---|
237 | * Formats the number following printf conventions. |
---|
238 | * Main limitation: Can only handle one format parameter at a time |
---|
239 | * Use multiple Format objects to format more than one number |
---|
240 | * @param s the format string following printf conventions |
---|
241 | * The string has a prefix, a format code and a suffix. The prefix and suffix |
---|
242 | * become part of the formatted output. The format code directs the |
---|
243 | * formatting of the (single) parameter to be formatted. The code has the |
---|
244 | * following structure |
---|
245 | * <ul> |
---|
246 | * <li> a % (required) |
---|
247 | * <li> a modifier (optional) |
---|
248 | * <dl> |
---|
249 | * <dt> + <dd> forces display of + for positive numbers |
---|
250 | * <dt> 0 <dd> show leading zeroes |
---|
251 | * <dt> - <dd> align left in the field |
---|
252 | * <dt> space <dd> prepend a space in front of positive numbers |
---|
253 | * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format. |
---|
254 | * </dl> |
---|
255 | * <li> an integer denoting field width (optional) |
---|
256 | * <li> a period followed by an integer denoting precision (optional) |
---|
257 | * <li> a format descriptor (required) |
---|
258 | * <dl> |
---|
259 | * <dt>f <dd> floating point number in fixed format |
---|
260 | * <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. |
---|
261 | * <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. |
---|
262 | * <dt>d, i <dd> integer in decimal |
---|
263 | * <dt>x <dd> integer in hexadecimal |
---|
264 | * <dt>o <dd> integer in octal |
---|
265 | * <dt>s <dd> string |
---|
266 | * <dt>c <dd> character |
---|
267 | * </dl> |
---|
268 | * </ul> |
---|
269 | * @exception IllegalArgumentException if bad format |
---|
270 | */ |
---|
271 | public Format(String s) { |
---|
272 | width = 0; precision = -1; |
---|
273 | pre = ""; |
---|
274 | post = ""; |
---|
275 | leading_zeroes = false; |
---|
276 | show_plus = false; |
---|
277 | alternate = false; |
---|
278 | show_space = false; |
---|
279 | left_align = false; |
---|
280 | fmt = ' '; |
---|
281 | |
---|
282 | int state = 0; |
---|
283 | int length = s.length(); |
---|
284 | int parse_state = 0; |
---|
285 | // 0 = prefix, 1 = flags, 2 = width, 3 = precision, |
---|
286 | // 4 = format, 5 = end |
---|
287 | int i = 0; |
---|
288 | |
---|
289 | while (parse_state == 0) |
---|
290 | { if (i >= length) parse_state = 5; |
---|
291 | else if (s.charAt(i) == '%') |
---|
292 | { if (i < length - 1) |
---|
293 | { if (s.charAt(i + 1) == '%') |
---|
294 | { pre = pre + '%'; |
---|
295 | i++; |
---|
296 | } |
---|
297 | else |
---|
298 | parse_state = 1; |
---|
299 | } |
---|
300 | else throw new java.lang.IllegalArgumentException(); |
---|
301 | } |
---|
302 | else |
---|
303 | pre = pre + s.charAt(i); |
---|
304 | i++; |
---|
305 | } |
---|
306 | while (parse_state == 1) |
---|
307 | { if (i >= length) parse_state = 5; |
---|
308 | else if (s.charAt(i) == ' ') show_space = true; |
---|
309 | else if (s.charAt(i) == '-') left_align = true; |
---|
310 | else if (s.charAt(i) == '+') show_plus = true; |
---|
311 | else if (s.charAt(i) == '0') leading_zeroes = true; |
---|
312 | else if (s.charAt(i) == '#') alternate = true; |
---|
313 | else { parse_state = 2; i--; } |
---|
314 | i++; |
---|
315 | } |
---|
316 | while (parse_state == 2) |
---|
317 | { if (i >= length) parse_state = 5; |
---|
318 | else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') |
---|
319 | { width = width * 10 + s.charAt(i) - '0'; |
---|
320 | i++; |
---|
321 | } |
---|
322 | else if (s.charAt(i) == '.') |
---|
323 | { parse_state = 3; |
---|
324 | precision = 0; |
---|
325 | i++; |
---|
326 | } |
---|
327 | else |
---|
328 | parse_state = 4; |
---|
329 | } |
---|
330 | while (parse_state == 3) |
---|
331 | { if (i >= length) parse_state = 5; |
---|
332 | else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') |
---|
333 | { precision = precision * 10 + s.charAt(i) - '0'; |
---|
334 | i++; |
---|
335 | } |
---|
336 | else |
---|
337 | parse_state = 4; |
---|
338 | } |
---|
339 | if (parse_state == 4) |
---|
340 | { if (i >= length) parse_state = 5; |
---|
341 | else fmt = s.charAt(i); |
---|
342 | i++; |
---|
343 | } |
---|
344 | if (i < length) |
---|
345 | post = s.substring(i, length); |
---|
346 | } |
---|
347 | /** |
---|
348 | * prints a formatted number following printf conventions |
---|
349 | * @param s a PrintStream |
---|
350 | * @param fmt the format string |
---|
351 | * @param x the double to print |
---|
352 | */ |
---|
353 | public static void print(java.io.PrintStream s,String fmt,double x) { |
---|
354 | s.print(new Format(fmt).form(x)); |
---|
355 | } |
---|
356 | /** |
---|
357 | * prints a formatted number following printf conventions |
---|
358 | * @param s a PrintStream |
---|
359 | * @param fmt the format string |
---|
360 | * @param x the long to print |
---|
361 | */ |
---|
362 | public static void print(java.io.PrintStream s,String fmt,long x) { |
---|
363 | s.print(new Format(fmt).form(x)); |
---|
364 | } |
---|
365 | /** |
---|
366 | * prints a formatted number following printf conventions |
---|
367 | * @param s a PrintStream |
---|
368 | * @param fmt the format string |
---|
369 | * @param x the character to |
---|
370 | */ |
---|
371 | public static void print(java.io.PrintStream s,String fmt,char x) { |
---|
372 | s.print(new Format(fmt).form(x)); |
---|
373 | } |
---|
374 | /** |
---|
375 | * prints a formatted number following printf conventions |
---|
376 | * @param s a PrintStream, fmt the format string |
---|
377 | * @param x a string that represents the digits to print |
---|
378 | */ |
---|
379 | public static void print(java.io.PrintStream s,String fmt,String x) { |
---|
380 | s.print(new Format(fmt).form(x)); |
---|
381 | } |
---|
382 | /** |
---|
383 | * Converts a string of digits (decimal, octal or hex) to an integer |
---|
384 | * @param s a string |
---|
385 | * @return the numeric value of the prefix of s representing a base 10 integer |
---|
386 | */ |
---|
387 | public static int atoi(String s) { |
---|
388 | return (int)atol(s); |
---|
389 | } |
---|
390 | /** |
---|
391 | * Converts a string of digits (decimal, octal or hex) to a long integer |
---|
392 | * @param s a string |
---|
393 | * @return the numeric value of the prefix of s representing a base 10 integer |
---|
394 | */ |
---|
395 | public static long atol(String s) { |
---|
396 | int i = 0; |
---|
397 | while (i < s.length() && Character.isWhitespace(s.charAt(i))) i++; |
---|
398 | if (i < s.length() && s.charAt(i) == '0') |
---|
399 | { if (i + 1 < s.length() && (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X')) |
---|
400 | return parseLong(s.substring(i + 2), 16); |
---|
401 | else return parseLong(s, 8); |
---|
402 | } |
---|
403 | else return parseLong(s, 10); |
---|
404 | } |
---|
405 | /** |
---|
406 | * Computes the format based on desired prescision and |
---|
407 | * minimum, maximum values. |
---|
408 | * |
---|
409 | * @param min minimum value |
---|
410 | * @param max maximum value |
---|
411 | * @param pres prescision |
---|
412 | */ |
---|
413 | public static String computeFormat(double min,double max,int pres) { |
---|
414 | double dx, xx; |
---|
415 | int ip, id, ib; |
---|
416 | String frmt; |
---|
417 | |
---|
418 | dx = Math.abs(max - min); |
---|
419 | xx = Math.abs(max); |
---|
420 | if(xx < Math.abs(min)) xx = Math.abs(min); |
---|
421 | if(pres >= 0 && (xx >= 1000000.0f || xx < 0.001f)) { |
---|
422 | id = pres -1; |
---|
423 | if(id < 0) id=0; |
---|
424 | frmt = "%" + "." + id + "e"; |
---|
425 | } else { |
---|
426 | ip = Math.abs(pres); |
---|
427 | id = ip - (int)(log10(dx) + 0.5); |
---|
428 | if(id < 0) id = 0; |
---|
429 | frmt = "%" + "." + id + "f"; |
---|
430 | } |
---|
431 | return frmt; |
---|
432 | } |
---|
433 | static final double log10(double x) { |
---|
434 | return 0.4342944819*Math.log(x); |
---|
435 | } |
---|
436 | /** |
---|
437 | * Converts a string of digits to an double |
---|
438 | * @param s a string |
---|
439 | */ |
---|
440 | public static double atof(String s) { |
---|
441 | int i = 0; int sign = 1; |
---|
442 | double r = 0; // integer part |
---|
443 | double f = 0; // fractional part |
---|
444 | double p = 1; // exponent of fractional part |
---|
445 | int state = 0; // 0 = int part, 1 = frac part |
---|
446 | |
---|
447 | while (i < s.length() && Character.isWhitespace(s.charAt(i))) i++; |
---|
448 | if (i < s.length() && s.charAt(i) == '-') { sign = -1; i++; } |
---|
449 | else if (i < s.length() && s.charAt(i) == '+') { i++; } |
---|
450 | while (i < s.length()) |
---|
451 | { char ch = s.charAt(i); |
---|
452 | if ('0' <= ch && ch <= '9') |
---|
453 | { if (state == 0) |
---|
454 | r = r * 10 + ch - '0'; |
---|
455 | else if (state == 1) |
---|
456 | { p = p / 10; |
---|
457 | r = r + p * (ch - '0'); |
---|
458 | } |
---|
459 | } |
---|
460 | else if (ch == '.') |
---|
461 | { if (state == 0) state = 1; |
---|
462 | else return sign * r; |
---|
463 | } |
---|
464 | else if (ch == 'e' || ch == 'E') |
---|
465 | { long e = (int)parseLong(s.substring(i + 1), 10); |
---|
466 | return sign * r * Math.pow(10, e); |
---|
467 | } |
---|
468 | else return sign * r; |
---|
469 | i++; |
---|
470 | } |
---|
471 | return sign * r; |
---|
472 | } |
---|
473 | /** |
---|
474 | * Formats a double into a string (like sprintf in C) |
---|
475 | * @param x the number to format |
---|
476 | * @return the formatted string |
---|
477 | * @exception IllegalArgumentException if bad argument |
---|
478 | */ |
---|
479 | public String form(double x) { |
---|
480 | String r; if (precision < 0) precision = 6; |
---|
481 | int s = 1; |
---|
482 | if (x < 0) { x = -x; s = -1; } |
---|
483 | if (fmt == 'f'){ |
---|
484 | r = fixed_format(x); |
---|
485 | } |
---|
486 | else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G') |
---|
487 | r = exp_format(x); |
---|
488 | else throw new java.lang.IllegalArgumentException(); |
---|
489 | return pad(sign(s, r)); |
---|
490 | } |
---|
491 | /** |
---|
492 | * Formats a long integer into a string (like sprintf in C) |
---|
493 | * @param x the number to format |
---|
494 | * @return the formatted string |
---|
495 | */ |
---|
496 | public String form(long x) { |
---|
497 | String r; int s = 0; |
---|
498 | if (fmt == 'd' || fmt == 'i') |
---|
499 | { s = 1; |
---|
500 | if (x < 0) { x = -x; s = -1; } |
---|
501 | r = "" + x; |
---|
502 | } |
---|
503 | else if (fmt == 'o') |
---|
504 | r = convert(x, 3, 7, "01234567"); |
---|
505 | else if (fmt == 'x') |
---|
506 | r = convert(x, 4, 15, "0123456789abcdef"); |
---|
507 | else if (fmt == 'X') |
---|
508 | r = convert(x, 4, 15, "0123456789ABCDEF"); |
---|
509 | else throw new java.lang.IllegalArgumentException(); |
---|
510 | |
---|
511 | return pad(sign(s, r)); |
---|
512 | } |
---|
513 | /** |
---|
514 | * Formats a character into a string (like sprintf in C) |
---|
515 | * @param x the value to format |
---|
516 | * @return the formatted string |
---|
517 | */ |
---|
518 | public String form(char c) { |
---|
519 | if (fmt != 'c') throw new java.lang.IllegalArgumentException(); |
---|
520 | |
---|
521 | String r = "" + c; |
---|
522 | return pad(r); |
---|
523 | } |
---|
524 | /** |
---|
525 | * Formats a string into a larger string (like sprintf in C) |
---|
526 | * @param x the value to format |
---|
527 | * @return the formatted string |
---|
528 | */ |
---|
529 | public String form(String s) { |
---|
530 | if (fmt != 's') throw new java.lang.IllegalArgumentException(); |
---|
531 | if (precision >= 0) s = s.substring(0, precision); |
---|
532 | return pad(s); |
---|
533 | } |
---|
534 | } |
---|