001: package org.geotools.misc;
002:
003: /** a utlity class to format numbers into strings.
004: * specify a length and number of decimal places.
005: * @author ian turton
006: */
007:
008: public class FormatedString {
009: static boolean leadingzeros = false;
010:
011: public static final String format(double d) {
012: return format("" + d, 2);
013: }
014:
015: public final static String format(int i) {
016: return format("" + i, 0);
017: }
018:
019: public final static String format(int i, int len) {
020: return format("" + i, 0, len);
021: }
022:
023: public static final String format(String in) {
024: return format(in, 2);
025: }
026:
027: public final static String format(String in, int dp, int len) {
028: StringBuffer sb = new StringBuffer();
029: String s = format(in, dp).trim();
030: int diff = len - s.length();
031:
032: for (int i = 0; i < diff; i++)
033: sb.append(" ");
034: sb.append(s);
035: return sb.toString();
036: }
037:
038: public final static String format(String in, int dp) {
039: int e1 = in.indexOf('e');
040: int e2 = in.indexOf('E');
041: int e = Math.max(e1, e2);
042: if (e > -1)
043: in = expand(in, e);
044: int i = in.lastIndexOf('.');
045: if (i != -1) {
046: String dec = "";
047: String num = in.substring(0, i);
048: if (dp > 0) {
049: if ((i + dp + 1) < in.length()) {
050: dec = in.substring(i, i + dp + 1);
051: } else {
052: dec = in.substring(i);
053:
054: }
055: } else {
056: dec = "";
057: }
058: while (dec.length() < dp + 1)
059: dec += "0";
060: if (dp == 0)
061: dec = "";
062: if (!leadingzeros) {
063: char[] tmp = num.toCharArray();
064: for (i = 0; i < tmp.length - 1; i++) {
065: if (tmp[i] != '0' && tmp[i] != ' ')
066: break;
067: if (tmp[i] == '0')
068: tmp[i] = ' ';
069: if (tmp[i + 1] == '.' && tmp[i] == ' ')
070: tmp[i] = '0';
071: }
072: num = new String(tmp);
073: }
074: return (num + dec);
075: } else {
076: String dec = ".";
077: while (dec.length() < dp + 1)
078: dec += "0";
079: if (dp == 0)
080: dec = "";
081:
082: if (!leadingzeros) {
083: char[] tmp = in.toCharArray();
084: for (i = 0; i < tmp.length - 1; i++) {
085: if (tmp[i] != '0' && tmp[i] != ' ')
086: break;
087: if (tmp[i] == '0')
088: tmp[i] = ' ';
089: if (tmp[i + 1] == '.' && tmp[i] == ' ')
090: tmp[i] = '0';
091: }
092: in = new String(tmp);
093: }
094: return (in + dec);
095: }
096: }
097:
098: private final static String expand(String s, int e) {
099: String last = s.substring(e + 1);
100: String start = s.substring(0, e);
101:
102: int pow = Integer.parseInt(last);
103: //System.out.println(start +" e "+last+" "+pow);
104: int i = start.indexOf('.');
105: if (i > 0) {
106: int d = start.length() - i - 1;
107: String a = start.substring(0, i);
108: start = a + start.substring(i + 1);
109: pow -= d;
110: }
111: for (i = 0; i < pow; i++) {
112: start += "0";
113: }
114: for (i = pow; i < 0; i++) {
115: start = "0" + start;
116: }
117: if (pow < 0) {
118: int lp = start.length() + pow;
119: //System.out.println(start+" x "+lp+" "+pow);
120: start = start.substring(0, lp) + "." + start.substring(lp);
121: }
122: //System.out.println("->"+start);
123:
124: return start.trim();
125: }
126:
127: public static void main(String args[]) {
128: System.out.println(args[0] + " "
129: + FormatedString.format(args[0], 6));
130: }
131: }
|