1 package net.sf.openrocket.util;
3 import java.util.Locale;
5 public class TextUtil {
8 * Return a string of the double value with suitable precision.
9 * The string is the shortest representation of the value including the
12 * TODO: MEDIUM: Extra zeros are added unnecessarily to the end of the string.
14 * @param d the value to present.
15 * @return a representation with suitable precision.
17 public static final String doubleToString(double d) {
19 // Check for special cases
20 if (MathUtil.equals(d, 0))
26 if (Double.isInfinite(d)) {
34 double abs = Math.abs(d);
37 // Compact exponential notation
45 String sign = (d < 0) ? "-" : "";
46 return sign + String.format((Locale)null, "%.4fe-%d", abs, exp);
49 return String.format((Locale)null, "%.7f", d);
51 return String.format((Locale)null, "%.6f", d);
53 return String.format((Locale)null, "%.5f", d);
55 return String.format((Locale)null, "%.4f", d);
57 return String.format((Locale)null, "%.3f", d);
59 return String.format((Locale)null, "%.2f", d);
61 return String.format((Locale)null, "%.1f", d);
62 if (abs < 100000000.0)
63 return String.format((Locale)null, "%.0f", d);
65 // Compact exponential notation
72 String sign = (d < 0) ? "-" : "";
73 return sign + String.format((Locale)null, "%.4fe%d", abs, exp);