3 * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $
6 * Copyright (C) 2005 The Jmol Development Team
8 * Contact: jmol-developers@lists.sf.net
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28 * created to remove ambiguities and make a simpler DecimalFormat
32 private final static String[] formattingStrings = { "0", "0.0", "0.00",
33 "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000",
35 private final static String zeros = "0000000000000000000000000000000000000000";
37 private final static float[] formatAdds = { 0.5f, 0.05f, 0.005f, 0.0005f,
38 0.00005f, 0.000005f, 0.0000005f, 0.00000005f, 0.000000005f, 0.0000000005f };
40 private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE };
42 public static void setUseNumberLocalization(boolean TF) {
43 useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE);
46 public static String formatDecimalDbl(double value, int decimalDigits) {
47 if (decimalDigits == Integer.MAX_VALUE
48 || value == Double.NEGATIVE_INFINITY
49 || value == Double.POSITIVE_INFINITY
50 || Double.isNaN(value))
52 return DF.formatDecimal((float) value, decimalDigits);
56 * a simple alternative to DecimalFormat (which Java2Script does not have
57 * and which is quite too complex for our use here.)
60 * @param decimalDigits
61 * @return formatted decimal
63 public static String formatDecimal(float value, int decimalDigits) {
64 if (decimalDigits == Integer.MAX_VALUE
65 || value == Float.NEGATIVE_INFINITY || value == Float.POSITIVE_INFINITY || Float.isNaN(value))
68 if (decimalDigits < 0) {
69 decimalDigits = -decimalDigits;
70 if (decimalDigits > formattingStrings.length)
71 decimalDigits = formattingStrings.length;
73 return formattingStrings[decimalDigits] + "E+0";
77 if (Math.abs(value) < 1) {
84 String s = ("" + d).toUpperCase();
85 int i = s.indexOf("E");
86 n = PT.parseInt(s.substring(i + 1)) + n;
87 return (i < 0 ? "" + value : formatDecimal(PT.parseFloat(s.substring(
88 0, i)), decimalDigits - 1)
89 + "E" + (n >= 0 ? "+" : "") + n);
92 if (decimalDigits >= formattingStrings.length)
93 decimalDigits = formattingStrings.length - 1;
94 String s1 = ("" + value).toUpperCase();
95 int pt = s1.indexOf(".");
96 if (pt < 0) // specifically JavaScript "-2" not "-2.0"
97 return s1 + formattingStrings[decimalDigits].substring(1);
98 boolean isNeg = s1.startsWith("-");
100 s1 = s1.substring(1);
103 int pt1 = s1.indexOf("E-");
105 n = PT.parseInt(s1.substring(pt1 + 1));
108 s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);
112 pt1 = s1.indexOf("E");
115 // 1.234E10 %3.8f -> 12340000000.00000000
117 n = PT.parseInt(s1.substring(pt1 + 1));
118 s1 = s1.substring(0, 1) + s1.substring(2, pt1) + zeros;
119 s1 = s1.substring(0, n + 1) + "." + s1.substring(n + 1);
120 pt = s1.indexOf(".");
122 // "234.345667 len == 10; pt = 3
123 // " 0.0 " decimalDigits = 1
125 int len = s1.length();
126 int pt2 = decimalDigits + pt + 1;
127 if (pt2 < len && s1.charAt(pt2) >= '5') {
128 return formatDecimal(
129 value + (isNeg ? -1 : 1) * formatAdds[decimalDigits], decimalDigits);
132 SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt
134 for (int i = 0; i < decimalDigits; i++, pt++) {
136 sb.appendC(s1.charAt(pt));
140 s1 = (isNeg ? "-" : "") + sb;
141 return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',
146 * an alternative to DecimalFormat "0.#"
150 * @return formatted number
152 public static String formatDecimalTrimmed(double x, int precision) {
153 String str = formatDecimalDbl(x, precision);
154 int m = str.length() - 1;
156 while (m >= 0 && str.charAt(m) == zero)
158 return str.substring(0, m + 1); // 0.##...