Numerous bug fixes and updates
[debian/openrocket] / src / net / sf / openrocket / util / TextUtil.java
1 package net.sf.openrocket.util;
2
3 import java.util.Locale;
4
5 public class TextUtil {
6
7         /**
8          * Return a string of the double value with suitable precision.
9          * The string is the shortest representation of the value including the
10          * required precision.
11          * 
12          * TODO: MEDIUM: Extra zeros are added unnecessarily to the end of the string.
13          * 
14          * @param d             the value to present.
15          * @return              a representation with suitable precision.
16          */
17         public static final String doubleToString(double d) {
18                 
19                 // Check for special cases
20                 if (MathUtil.equals(d, 0))
21                         return "0";
22                 
23                 if (Double.isNaN(d))
24                         return "NaN";
25                 
26                 if (Double.isInfinite(d)) {
27                         if (d < 0)
28                                 return "-Inf";
29                         else
30                                 return "Inf";
31                 }
32                 
33                 
34                 double abs = Math.abs(d);
35                 
36                 if (abs < 0.001) {
37                         // Compact exponential notation
38                         int exp = 0;
39                         
40                         while (abs < 1.0) {
41                                 abs *= 10;
42                                 exp++;
43                         }
44                         
45                         String sign = (d < 0) ? "-" : "";
46                         return sign + String.format((Locale)null, "%.4fe-%d", abs, exp);
47                 }
48                 if (abs < 0.01)
49                         return String.format((Locale)null, "%.7f", d);
50                 if (abs < 0.1)
51                         return String.format((Locale)null, "%.6f", d);
52                 if (abs < 1)
53                         return String.format((Locale)null, "%.5f", d);
54                 if (abs < 10)
55                         return String.format((Locale)null, "%.4f", d);
56                 if (abs < 100)
57                         return String.format((Locale)null, "%.3f", d);
58                 if (abs < 1000)
59                         return String.format((Locale)null, "%.2f", d);
60                 if (abs < 10000)
61                         return String.format((Locale)null, "%.1f", d);
62                 if (abs < 100000000.0)
63                         return String.format((Locale)null, "%.0f", d);
64                         
65                 // Compact exponential notation
66                 int exp = 0;
67                 while (abs >= 10.0) {
68                         abs /= 10;
69                         exp++;
70                 }
71                 
72                 String sign = (d < 0) ? "-" : "";
73                 return sign + String.format((Locale)null, "%.4fe%d", abs, exp);
74         }
75
76 }