Source Code Cross Referenced for Fmt.java in  » GIS » openjump » com » vividsolutions » jump » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » openjump » com.vividsolutions.jump.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI 
003:         * for visualizing and manipulating spatial features with geometry and attributes.
004:         *
005:         * Copyright (C) 2003 Vivid Solutions
006:         * 
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License
009:         * as published by the Free Software Foundation; either version 2
010:         * of the License, or (at your option) any later version.
011:         * 
012:         * This program is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015:         * GNU General Public License for more details.
016:         * 
017:         * You should have received a copy of the GNU General Public License
018:         * along with this program; if not, write to the Free Software
019:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020:         * 
021:         * For more information, contact:
022:         *
023:         * Vivid Solutions
024:         * Suite #1A
025:         * 2328 Government Street
026:         * Victoria BC  V8T 5G5
027:         * Canada
028:         *
029:         * (250)385-6040
030:         * www.vividsolutions.com
031:         */
032:
033:        package com.vividsolutions.jump.util;
034:
035:        // Fmt - some simple single-arg sprintf-like routines
036:        //
037:        // Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>.  All rights reserved.
038:        //
039:        // Redistribution and use in source and binary forms, with or without
040:        // modification, are permitted provided that the following conditions
041:        // are met:
042:        // 1. Redistributions of source code must retain the above copyright
043:        //    notice, this list of conditions and the following disclaimer.
044:        // 2. Redistributions in binary form must reproduce the above copyright
045:        //    notice, this list of conditions and the following disclaimer in the
046:        //    documentation and/or other materials provided with the distribution.
047:        //
048:        // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
049:        // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
050:        // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
051:        // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
052:        // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
053:        // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
054:        // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
055:        // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
056:        // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
057:        // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
058:        // SUCH DAMAGE.
059:        //
060:        // Visit the ACME Labs Java page for up-to-date versions of this and other
061:        // fine Java utilities: http://www.acme.com/java/
062:        /// Some simple single-arg sprintf-like routines.
063:        // <P>
064:        // It is apparently impossible to declare a Java method that accepts
065:        // variable numbers of any type of argument.  You can declare it to take
066:        // Objects, but numeric variables and constants are not in fact Objects.
067:        // <P>
068:        // However, using the built-in string concatenation, it's almost as
069:        // convenient to make a series of single-argument formatting routines.
070:        // <P>
071:        // Fmt can format the following types:
072:        // <BLOCKQUOTE><CODE>
073:        // byte short int long float double char String Object
074:        // </CODE></BLOCKQUOTE>
075:        // For each type there is a set of overloaded methods, each returning
076:        // a formatted String.  There's the plain formatting version:
077:        // <BLOCKQUOTE><PRE>
078:        // Fmt.fmt( x )
079:        // </PRE></BLOCKQUOTE>
080:        // There's a version specifying a minimum field width:
081:        // <BLOCKQUOTE><PRE>
082:        // Fmt.fmt( x, minWidth )
083:        // </PRE></BLOCKQUOTE>
084:        // And there's a version that takes flags:
085:        // <BLOCKQUOTE><PRE>
086:        // Fmt.fmt( x, minWidth, flags )
087:        // </PRE></BLOCKQUOTE>
088:        // Currently available flags are:
089:        // <BLOCKQUOTE><PRE>
090:        // Fmt.ZF - zero-fill
091:        // Fmt.LJ - left justify
092:        // Fmt.HX - hexadecimal
093:        // Fmt.OC - octal
094:        // </PRE></BLOCKQUOTE>
095:        // The HX and OC flags imply unsigned output.
096:        // <P>
097:        // For doubles and floats, there's a significant-figures parameter before
098:        // the flags:
099:        // <BLOCKQUOTE><PRE>
100:        // Fmt.fmt( d )
101:        // Fmt.fmt( d, minWidth )
102:        // Fmt.fmt( d, minWidth, sigFigs )
103:        // Fmt.fmt( d, minWidth, sigFigs, flags )
104:        // </PRE></BLOCKQUOTE>
105:        // <P>
106:        // <A HREF="/resources/classes/Acme/Fmt.java">Fetch the software.</A><BR>
107:        // <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
108:        // <HR>
109:        // Similar classes:
110:        // <UL>
111:        // <LI> Andrew Scherpbier's <A HREF="http://www.sdsu.edu/doc/java-SDSU/sdsu.FormatString.html">FormatString</A>
112:        // Tries to allow variable numbers of arguments by
113:        // supplying overloaded routines with different combinations of parameters,
114:        // but doesn't actually supply that many.  The floating point conversion
115:        // is described as "very incomplete".
116:        // <LI> Core Java's <A HREF="http://www.apl.jhu.edu/~hall/java/CoreJava-Format.html">Format</A>.
117:        // The design seems a little weird.  They want you to create an instance,
118:        // passing the format string to the constructor, and then call an instance
119:        // method with your data to do the actual formatting.  The extra steps are
120:        // pointless; better to just use static methods.
121:        // </UL>
122:        public class Fmt {
123:            // Flags.
124:            /// Zero-fill.
125:            public static final int ZF = 1;
126:
127:            /// Left justify.
128:            public static final int LJ = 2;
129:
130:            /// Hexadecimal.
131:            public static final int HX = 4;
132:
133:            /// Octal.
134:            public static final int OC = 8;
135:
136:            // Was a number - internal use.
137:            private static final int WN = 16;
138:
139:            // byte
140:            public static String fmt(byte b) {
141:                return fmt(b, 0, 0);
142:            }
143:
144:            public static String fmt(byte b, int minWidth) {
145:                return fmt(b, minWidth, 0);
146:            }
147:
148:            public static String fmt(byte b, int minWidth, int flags) {
149:                boolean hexadecimal = ((flags & HX) != 0);
150:                boolean octal = ((flags & OC) != 0);
151:
152:                if (hexadecimal) {
153:                    return fmt(Integer.toString(b & 0xff, 16), minWidth, flags
154:                            | WN);
155:                } else if (octal) {
156:                    return fmt(Integer.toString(b & 0xff, 8), minWidth, flags
157:                            | WN);
158:                } else {
159:                    return fmt(Integer.toString(b & 0xff), minWidth, flags | WN);
160:                }
161:            }
162:
163:            // short
164:            public static String fmt(short s) {
165:                return fmt(s, 0, 0);
166:            }
167:
168:            public static String fmt(short s, int minWidth) {
169:                return fmt(s, minWidth, 0);
170:            }
171:
172:            public static String fmt(short s, int minWidth, int flags) {
173:                boolean hexadecimal = ((flags & HX) != 0);
174:                boolean octal = ((flags & OC) != 0);
175:
176:                if (hexadecimal) {
177:                    return fmt(Integer.toString(s & 0xffff, 16), minWidth,
178:                            flags | WN);
179:                } else if (octal) {
180:                    return fmt(Integer.toString(s & 0xffff, 8), minWidth, flags
181:                            | WN);
182:                } else {
183:                    return fmt(Integer.toString(s), minWidth, flags | WN);
184:                }
185:            }
186:
187:            // int
188:            public static String fmt(int i) {
189:                return fmt(i, 0, 0);
190:            }
191:
192:            public static String fmt(int i, int minWidth) {
193:                return fmt(i, minWidth, 0);
194:            }
195:
196:            public static String fmt(int i, int minWidth, int flags) {
197:                boolean hexadecimal = ((flags & HX) != 0);
198:                boolean octal = ((flags & OC) != 0);
199:
200:                if (hexadecimal) {
201:                    return fmt(Long.toString(i & 0xffffffffL, 16), minWidth,
202:                            flags | WN);
203:                } else if (octal) {
204:                    return fmt(Long.toString(i & 0xffffffffL, 8), minWidth,
205:                            flags | WN);
206:                } else {
207:                    return fmt(Integer.toString(i), minWidth, flags | WN);
208:                }
209:            }
210:
211:            // long
212:            public static String fmt(long l) {
213:                return fmt(l, 0, 0);
214:            }
215:
216:            public static String fmt(long l, int minWidth) {
217:                return fmt(l, minWidth, 0);
218:            }
219:
220:            public static String fmt(long l, int minWidth, int flags) {
221:                boolean hexadecimal = ((flags & HX) != 0);
222:                boolean octal = ((flags & OC) != 0);
223:
224:                if (hexadecimal) {
225:                    if ((l & 0xf000000000000000L) != 0) {
226:                        return fmt(Long.toString(l >>> 60, 16)
227:                                + fmt(l & 0x0fffffffffffffffL, 15, HX | ZF),
228:                                minWidth, flags | WN);
229:                    } else {
230:                        return fmt(Long.toString(l, 16), minWidth, flags | WN);
231:                    }
232:                } else if (octal) {
233:                    if ((l & 0x8000000000000000L) != 0) {
234:                        return fmt(Long.toString(l >>> 63, 8)
235:                                + fmt(l & 0x7fffffffffffffffL, 21, OC | ZF),
236:                                minWidth, flags | WN);
237:                    } else {
238:                        return fmt(Long.toString(l, 8), minWidth, flags | WN);
239:                    }
240:                } else {
241:                    return fmt(Long.toString(l), minWidth, flags | WN);
242:                }
243:            }
244:
245:            // float
246:            public static String fmt(float f) {
247:                return fmt(f, 0, 0, 0);
248:            }
249:
250:            public static String fmt(float f, int minWidth) {
251:                return fmt(f, minWidth, 0, 0);
252:            }
253:
254:            public static String fmt(float f, int minWidth, int sigFigs) {
255:                return fmt(f, minWidth, sigFigs, 0);
256:            }
257:
258:            public static String fmt(float f, int minWidth, int sigFigs,
259:                    int flags) {
260:                if (sigFigs != 0) {
261:                    return fmt(sigFigFix(Float.toString(f), sigFigs), minWidth,
262:                            flags | WN);
263:                } else {
264:                    return fmt(Float.toString(f), minWidth, flags | WN);
265:                }
266:            }
267:
268:            // double
269:            public static String fmt(double d) {
270:                return fmt(d, 0, 0, 0);
271:            }
272:
273:            public static String fmt(double d, int minWidth) {
274:                return fmt(d, minWidth, 0, 0);
275:            }
276:
277:            public static String fmt(double d, int minWidth, int sigFigs) {
278:                return fmt(d, minWidth, sigFigs, 0);
279:            }
280:
281:            public static String fmt(double d, int minWidth, int sigFigs,
282:                    int flags) {
283:                if (sigFigs != 0) {
284:                    return fmt(sigFigFix(doubleToString(d), sigFigs), minWidth,
285:                            flags | WN);
286:                } else {
287:                    return fmt(doubleToString(d), minWidth, flags | WN);
288:                }
289:            }
290:
291:            // char
292:            public static String fmt(char c) {
293:                return fmt(c, 0, 0);
294:            }
295:
296:            public static String fmt(char c, int minWidth) {
297:                return fmt(c, minWidth, 0);
298:            }
299:
300:            public static String fmt(char c, int minWidth, int flags) {
301:                // return fmt( Character.toString( c ), minWidth, flags );
302:                // Character currently lacks a static toString method.  Workaround
303:                // is to make a temporary instance and use the instance toString.
304:                return fmt(new Character(c).toString(), minWidth, flags);
305:            }
306:
307:            // Object
308:            public static String fmt(Object o) {
309:                return fmt(o, 0, 0);
310:            }
311:
312:            public static String fmt(Object o, int minWidth) {
313:                return fmt(o, minWidth, 0);
314:            }
315:
316:            public static String fmt(Object o, int minWidth, int flags) {
317:                return fmt(o.toString(), minWidth, flags);
318:            }
319:
320:            // String
321:            public static String fmt(String s) {
322:                return fmt(s, 0, 0);
323:            }
324:
325:            public static String fmt(String s, int minWidth) {
326:                return fmt(s, minWidth, 0);
327:            }
328:
329:            public static String fmt(String s, int minWidth, int flags) {
330:                int len = s.length();
331:                boolean zeroFill = ((flags & ZF) != 0);
332:                boolean leftJustify = ((flags & LJ) != 0);
333:                boolean hexadecimal = ((flags & HX) != 0);
334:                boolean octal = ((flags & OC) != 0);
335:                boolean wasNumber = ((flags & WN) != 0);
336:
337:                if ((hexadecimal || octal || zeroFill) && !wasNumber) {
338:                    throw new InternalError(
339:                            "Acme.Fmt: number flag on a non-number");
340:                }
341:
342:                if (zeroFill && leftJustify) {
343:                    throw new InternalError(
344:                            "Acme.Fmt: zero-fill left-justify is silly");
345:                }
346:
347:                if (hexadecimal && octal) {
348:                    throw new InternalError(
349:                            "Acme.Fmt: can't do both hex and octal");
350:                }
351:
352:                if (len >= minWidth) {
353:                    return s;
354:                }
355:
356:                int fillWidth = minWidth - len;
357:                StringBuffer fill = new StringBuffer(fillWidth);
358:
359:                for (int i = 0; i < fillWidth; ++i)
360:                    if (zeroFill) {
361:                        fill.append('0');
362:                    } else {
363:                        fill.append(' ');
364:                    }
365:
366:                if (leftJustify) {
367:                    return s + fill;
368:                } else if (zeroFill && s.startsWith("-")) {
369:                    return "-" + fill + s.substring(1);
370:                } else {
371:                    return fill + s;
372:                }
373:            }
374:
375:            // Internal routines.
376:            private static String sigFigFix(String s, int sigFigs) {
377:                // First dissect the floating-point number string into sign,
378:                // integer part, fraction part, and exponent.
379:                String sign;
380:                String unsigned;
381:
382:                if (s.startsWith("-") || s.startsWith("+")) {
383:                    sign = s.substring(0, 1);
384:                    unsigned = s.substring(1);
385:                } else {
386:                    sign = "";
387:                    unsigned = s;
388:                }
389:
390:                String mantissa;
391:                String exponent;
392:                int eInd = unsigned.indexOf('e');
393:
394:                if (eInd == -1) { // it may be 'e' or 'E'
395:                    eInd = unsigned.indexOf('E');
396:                }
397:
398:                if (eInd == -1) {
399:                    mantissa = unsigned;
400:                    exponent = "";
401:                } else {
402:                    mantissa = unsigned.substring(0, eInd);
403:                    exponent = unsigned.substring(eInd);
404:                }
405:
406:                StringBuffer number;
407:                StringBuffer fraction;
408:                int dotInd = mantissa.indexOf('.');
409:
410:                if (dotInd == -1) {
411:                    number = new StringBuffer(mantissa);
412:                    fraction = new StringBuffer("");
413:                } else {
414:                    number = new StringBuffer(mantissa.substring(0, dotInd));
415:                    fraction = new StringBuffer(mantissa.substring(dotInd + 1));
416:                }
417:
418:                int numFigs = number.length();
419:                int fracFigs = fraction.length();
420:
421:                if (((numFigs == 0) || number.equals("0")) && (fracFigs > 0)) {
422:                    // Don't count leading zeros in the fraction.
423:                    numFigs = 0;
424:
425:                    for (int i = 0; i < fraction.length(); ++i) {
426:                        if (fraction.charAt(i) != '0') {
427:                            break;
428:                        }
429:
430:                        --fracFigs;
431:                    }
432:                }
433:
434:                int mantFigs = numFigs + fracFigs;
435:
436:                if (sigFigs > mantFigs) {
437:                    // We want more figures; just append zeros to the fraction.
438:                    for (int i = mantFigs; i < sigFigs; ++i)
439:                        fraction.append('0');
440:                } else if ((sigFigs < mantFigs) && (sigFigs >= numFigs)) {
441:                    // Want fewer figures in the fraction; chop.
442:                    fraction.setLength(fraction.length()
443:                            - (fracFigs - (sigFigs - numFigs)));
444:
445:                    // Round?
446:                } else if (sigFigs < numFigs) {
447:                    // Want fewer figures in the number; turn them to zeros.
448:                    fraction.setLength(0); // should already be zero, but make sure
449:
450:                    for (int i = sigFigs; i < numFigs; ++i)
451:                        number.setCharAt(i, '0');
452:
453:                    // Round?
454:                }
455:
456:                // Else sigFigs == mantFigs, which is fine.
457:                if (fraction.length() == 0) {
458:                    return sign + number + exponent;
459:                } else {
460:                    return sign + number + "." + fraction + exponent;
461:                }
462:            }
463:
464:            /// Improved version of Double.toString(), returns more decimal places.
465:            // <P>
466:            // The JDK 1.0.2 version of Double.toString() returns only six decimal
467:            // places on some systems.  In JDK 1.1 full precision is returned on
468:            // all platforms.
469:            // @deprecated
470:            // @see java.lang.Double#toString
471:            public static String doubleToString(double d) {
472:                // Handle special numbers first, to avoid complications.
473:                if (Double.isNaN(d)) {
474:                    return "NaN";
475:                }
476:
477:                if (d == Double.NEGATIVE_INFINITY) {
478:                    return "-Inf";
479:                }
480:
481:                if (d == Double.POSITIVE_INFINITY) {
482:                    return "Inf";
483:                }
484:
485:                // Grab the sign, and then make the number positive for simplicity.
486:                boolean negative = false;
487:
488:                if (d < 0.0D) {
489:                    negative = true;
490:                    d = -d;
491:                }
492:
493:                // Get the native version of the unsigned value, as a template.
494:                String unsStr = Double.toString(d);
495:
496:                // Dissect out the exponent.
497:                String mantStr;
498:
499:                // Dissect out the exponent.
500:                String expStr;
501:                int exp;
502:                int eInd = unsStr.indexOf('e');
503:
504:                if (eInd == -1) { // it may be 'e' or 'E'
505:                    eInd = unsStr.indexOf('E');
506:                }
507:
508:                if (eInd == -1) {
509:                    mantStr = unsStr;
510:                    expStr = "";
511:                    exp = 0;
512:                } else {
513:                    mantStr = unsStr.substring(0, eInd);
514:                    expStr = unsStr.substring(eInd + 1);
515:
516:                    if (expStr.startsWith("+")) {
517:                        exp = Integer.parseInt(expStr.substring(1));
518:                    } else {
519:                        exp = Integer.parseInt(expStr);
520:                    }
521:                }
522:
523:                // Dissect out the number part.
524:                String numStr;
525:                int dotInd = mantStr.indexOf('.');
526:
527:                if (dotInd == -1) {
528:                    numStr = mantStr;
529:                } else {
530:                    numStr = mantStr.substring(0, dotInd);
531:                }
532:
533:                long num;
534:
535:                if (numStr.length() == 0) {
536:                    num = 0;
537:                } else {
538:                    num = Integer.parseInt(numStr);
539:                }
540:
541:                // Build the new mantissa.
542:                StringBuffer newMantBuf = new StringBuffer(numStr + ".");
543:                double p = Math.pow(10, exp);
544:                double frac = d - (num * p);
545:                String digits = "0123456789";
546:                int nDigits = 16 - numStr.length(); // about 16 digits in a double
547:
548:                for (int i = 0; i < nDigits; ++i) {
549:                    p /= 10.0D;
550:
551:                    int dig = (int) (frac / p);
552:
553:                    if (dig < 0) {
554:                        dig = 0;
555:                    }
556:
557:                    if (dig > 9) {
558:                        dig = 9;
559:                    }
560:
561:                    newMantBuf.append(digits.charAt(dig));
562:                    frac -= (dig * p);
563:                }
564:
565:                if ((int) ((frac / p) + 0.5D) == 1) {
566:                    // Round up.
567:                    boolean roundMore = true;
568:
569:                    for (int i = newMantBuf.length() - 1; i >= 0; --i) {
570:                        int dig = digits.indexOf(newMantBuf.charAt(i));
571:
572:                        if (dig == -1) {
573:                            continue;
574:                        }
575:
576:                        ++dig;
577:
578:                        if (dig == 10) {
579:                            newMantBuf.setCharAt(i, '0');
580:
581:                            continue;
582:                        }
583:
584:                        newMantBuf.setCharAt(i, digits.charAt(dig));
585:                        roundMore = false;
586:
587:                        break;
588:                    }
589:
590:                    if (roundMore) {
591:                        // If this happens, we need to prepend a 1.  But I haven't
592:                        // found a test case yet, so I'm leaving it out for now.
593:                        // But if you get this message, please let me know!
594:                        newMantBuf.append("ROUNDMORE");
595:                    }
596:                }
597:
598:                // Chop any trailing zeros.
599:                int len = newMantBuf.length();
600:
601:                while (newMantBuf.charAt(len - 1) == '0')
602:                    newMantBuf.setLength(--len);
603:
604:                // And chop a trailing dot, if any.
605:                if (newMantBuf.charAt(len - 1) == '.') {
606:                    newMantBuf.setLength(--len);
607:                }
608:
609:                // Done.
610:                return (negative ? "-" : "") + newMantBuf
611:                        + ((expStr.length() != 0) ? ("e" + expStr) : "");
612:            }
613:
614:            /******************************************************************************
615:                /// Test program.
616:                public static void main( String[] args )
617:                    {
618:                    System.out.println( "Starting tests." );
619:                    show( Fmt.fmt( "Hello there." ) );
620:                    show( Fmt.fmt( 123 ) );
621:                    show( Fmt.fmt( 123, 10 ) );
622:                    show( Fmt.fmt( 123, 10, Fmt.ZF ) );
623:                    show( Fmt.fmt( 123, 10, Fmt.LJ ) );
624:                    show( Fmt.fmt( -123 ) );
625:                    show( Fmt.fmt( -123, 10 ) );
626:                    show( Fmt.fmt( -123, 10, Fmt.ZF ) );
627:                    show( Fmt.fmt( -123, 10, Fmt.LJ ) );
628:                    show( Fmt.fmt( (byte) 0xbe, 22, Fmt.OC ) );
629:                    show( Fmt.fmt( (short) 0xbabe, 22, Fmt.OC ) );
630:                    show( Fmt.fmt( 0xcafebabe, 22, Fmt.OC ) );
631:                    show( Fmt.fmt( 0xdeadbeefcafebabeL, 22, Fmt.OC ) );
632:                    show( Fmt.fmt( 0x8000000000000000L, 22, Fmt.OC ) );
633:                    show( Fmt.fmt( (byte) 0xbe, 16, Fmt.HX ) );
634:                    show( Fmt.fmt( (short) 0xbabe, 16, Fmt.HX ) );
635:                    show( Fmt.fmt( 0xcafebabe, 16, Fmt.HX ) );
636:                    show( Fmt.fmt( 0xdeadbeefcafebabeL, 16, Fmt.HX ) );
637:                    show( Fmt.fmt( 0x8000000000000000L, 16, Fmt.HX ) );
638:                    show( Fmt.fmt( 'c' ) );
639:                    show( Fmt.fmt( new java.util.Date() ) );
640:                    show( Fmt.fmt( 123.456F ) );
641:                    show( Fmt.fmt( 123456000000000000.0F ) );
642:                    show( Fmt.fmt( 123.456F, 0, 8 ) );
643:                    show( Fmt.fmt( 123.456F, 0, 7 ) );
644:                    show( Fmt.fmt( 123.456F, 0, 6 ) );
645:                    show( Fmt.fmt( 123.456F, 0, 5 ) );
646:                    show( Fmt.fmt( 123.456F, 0, 4 ) );
647:                    show( Fmt.fmt( 123.456F, 0, 3 ) );
648:                    show( Fmt.fmt( 123.456F, 0, 2 ) );
649:                    show( Fmt.fmt( 123.456F, 0, 1 ) );
650:                    show( Fmt.fmt( 123456000000000000.0F, 0, 4 ) );
651:                    show( Fmt.fmt( -123.456F, 0, 4 ) );
652:                    show( Fmt.fmt( -123456000000000000.0F, 0, 4 ) );
653:                    show( Fmt.fmt( 123.0F ) );
654:                    show( Fmt.fmt( 123.0D ) );
655:                    show( Fmt.fmt( 1.234567890123456789F ) );
656:                    show( Fmt.fmt( 1.234567890123456789D ) );
657:                    show( Fmt.fmt( 1234567890123456789F ) );
658:                    show( Fmt.fmt( 1234567890123456789D ) );
659:                    show( Fmt.fmt( 0.000000000000000000001234567890123456789F ) );
660:                    show( Fmt.fmt( 0.000000000000000000001234567890123456789D ) );
661:                    show( Fmt.fmt( 12300.0F ) );
662:                    show( Fmt.fmt( 12300.0D ) );
663:                    show( Fmt.fmt( 123000.0F ) );
664:                    show( Fmt.fmt( 123000.0D ) );
665:                    show( Fmt.fmt( 1230000.0F ) );
666:                    show( Fmt.fmt( 1230000.0D ) );
667:                    show( Fmt.fmt( 12300000.0F ) );
668:                    show( Fmt.fmt( 12300000.0D ) );
669:                    show( Fmt.fmt( Float.NaN ) );
670:                    show( Fmt.fmt( Float.POSITIVE_INFINITY ) );
671:                    show( Fmt.fmt( Float.NEGATIVE_INFINITY ) );
672:                    show( Fmt.fmt( Double.NaN ) );
673:                    show( Fmt.fmt( Double.POSITIVE_INFINITY ) );
674:                    show( Fmt.fmt( Double.NEGATIVE_INFINITY ) );
675:                    show( Fmt.fmt( 1.0F / 8.0F ) );
676:                    show( Fmt.fmt( 1.0D / 8.0D ) );
677:                    System.out.println( "Done with tests." );
678:                    }
679:
680:                private static void show( String str )
681:                    {
682:                    System.out.println( "#" + str + "#" );
683:                    }
684:             ******************************************************************************/
685:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.