updates for 0.9.3
[debian/openrocket] / src / net / sf / openrocket / util / TextUtil.java
1 package net.sf.openrocket.util;
2
3
4 public class TextUtil {
5
6         /**
7          * Return a string of the double value with suitable precision (5 digits).
8          * The string is the shortest representation of the value including the
9          * required precision.
10          * 
11          * @param d             the value to present.
12          * @return              a representation with suitable precision.
13          */
14         public static final String doubleToString(double d) {
15                 
16                 // Check for special cases
17                 if (MathUtil.equals(d, 0))
18                         return "0";
19                 
20                 if (Double.isNaN(d))
21                         return "NaN";
22                 
23                 if (Double.isInfinite(d)) {
24                         if (d < 0)
25                                 return "-Inf";
26                         else
27                                 return "Inf";
28                 }
29                 
30                 
31                 final String sign = (d < 0) ? "-" : "";
32                 double abs = Math.abs(d);
33                 
34                 // Small and large values always in exponential notation
35                 if (abs < 0.001 || abs >= 100000000) {
36                         return sign + exponentialFormat(abs);
37                 }
38                 
39                 // Check whether decimal or exponential notation is shorter
40                 
41                 String exp = exponentialFormat(abs);
42                 String dec = decimalFormat(abs);
43                 
44                 if (dec.length() <= exp.length())
45                         return sign + dec;
46                 else
47                         return sign + exp;
48         }
49         
50         
51         /*
52          * value must be positive and not zero!
53          */
54         private static String exponentialFormat(double value) {
55                 int exp;
56                 
57                 exp = 0;
58                 while (value < 1.0) {
59                         value *= 10;
60                         exp--;
61                 }
62                 while (value >= 10.0) {
63                         value /= 10;
64                         exp++;
65                 }
66                 
67                 return shortDecimal(value, 4) + "e" + exp;
68         }
69         
70         
71         /*
72          * value must be positive and not zero!
73          */
74         private static String decimalFormat(double value) {
75                 if (value >= 10000)
76                         return "" + (int)(value + 0.5);
77                 
78                 int decimals = 1;
79                 double v = value;
80                 while (v < 1000) {
81                         v *= 10;
82                         decimals++;
83                 }
84                 
85                 return shortDecimal(value, decimals);
86         }
87         
88         
89         
90         
91         /*
92          * value must be positive!
93          */
94         private static String shortDecimal(double value, int decimals) {
95                 
96                 int whole = (int)value;
97                 value -= whole;
98                 
99                 // Calculate limit, return when remaining value less than this
100                 double limit;
101                 limit = 0.5;
102                 for (int i=0; i<decimals; i++)
103                         limit /= 10;
104                 
105                 
106                 if (value < limit)
107                         return "" + whole; 
108                 limit *= 10;
109
110                 StringBuilder sb = new StringBuilder();
111                 sb.append("" + whole);
112                 sb.append('.');
113
114                 
115                 for (int i = 0; i<decimals; i++) {
116                         
117                         value *= 10;
118                         if (i == decimals-1)
119                                 value += 0.5;
120                         whole = (int)value;
121                         value -= whole;
122                         sb.append((char)('0' + whole));
123                         
124                         if (value < limit)
125                                 return sb.toString();
126                         limit *= 10;
127
128                 }
129
130                 return sb.toString();
131         }
132
133 }