3 * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $
6 * Some portions of this file have been modified by Robert Hanson hansonr.at.stolaf.edu 2012-2017
7 * for use in SwingJS via transpilation into JavaScript using Java2Script.
9 * Copyright (C) 2005 The Jmol Development Team
11 * Contact: jmol-developers@lists.sf.net
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31 * created to remove ambiguities and make a simpler DecimalFormat
35 private final static String[] formattingStrings = { "0", "0.0", "0.00",
36 "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000",
38 private final static String zeros = "0000000000000000000000000000000000000000";
40 private final static float[] formatAdds = { 0.5f, 0.05f, 0.005f, 0.0005f,
41 0.00005f, 0.000005f, 0.0000005f, 0.00000005f, 0.000000005f, 0.0000000005f };
43 private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE };
45 public static void setUseNumberLocalization(boolean TF) {
46 useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE);
49 public static String formatDecimalDbl(double value, int decimalDigits) {
50 if (decimalDigits == Integer.MAX_VALUE
51 || value == Double.NEGATIVE_INFINITY
52 || value == Double.POSITIVE_INFINITY
53 || Double.isNaN(value))
55 return DF.formatDecimal((float) value, decimalDigits);
59 * a simple alternative to DecimalFormat (which Java2Script does not have
60 * and which is quite too complex for our use here.)
63 * @param decimalDigits
64 * @return formatted decimal
66 public static String formatDecimal(float value, int decimalDigits) {
67 if (decimalDigits == Integer.MAX_VALUE
68 || value == Float.NEGATIVE_INFINITY || value == Float.POSITIVE_INFINITY || Float.isNaN(value))
71 if (decimalDigits < 0) {
72 decimalDigits = -decimalDigits;
73 if (decimalDigits > formattingStrings.length)
74 decimalDigits = formattingStrings.length;
76 return formattingStrings[decimalDigits - 1] + "E+0";
80 if (Math.abs(value) < 1) {
87 String s = ("" + d).toUpperCase();
88 int i = s.indexOf("E");
89 n = PT.parseInt(s.substring(i + 1)) + n;
94 float f = PT.parseFloat(s.substring(0, i));
95 if (f == 10 || f == -10) {
96 //d = 9.99999997465; n = -6 --> 10.00000E-5
98 n += (n < 0 ? 1 : -1);
100 sf = formatDecimal(f, decimalDigits - 1);
102 return sf + "E" + (n >= 0 ? "+" : "") + n;
105 if (decimalDigits >= formattingStrings.length)
106 decimalDigits = formattingStrings.length - 1;
107 String s1 = ("" + value).toUpperCase();
108 int pt = s1.indexOf(".");
109 if (pt < 0) // specifically JavaScript "-2" not "-2.0"
110 return s1 + formattingStrings[decimalDigits].substring(1);
111 boolean isNeg = s1.startsWith("-");
113 s1 = s1.substring(1);
116 int pt1 = s1.indexOf("E-");
118 n = PT.parseInt(s1.substring(pt1 + 1));
121 s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);
125 pt1 = s1.indexOf("E");
128 // 1.234E10 %3.8f -> 12340000000.00000000
130 n = PT.parseInt(s1.substring(pt1 + 1));
131 s1 = s1.substring(0, 1) + s1.substring(2, pt1) + zeros;
132 s1 = s1.substring(0, n + 1) + "." + s1.substring(n + 1);
133 pt = s1.indexOf(".");
135 // "234.345667 len == 10; pt = 3
136 // " 0.0 " decimalDigits = 1
138 int len = s1.length();
139 int pt2 = decimalDigits + pt + 1;
140 if (pt2 < len && s1.charAt(pt2) >= '5') {
141 return formatDecimal(
142 value + (isNeg ? -1 : 1) * formatAdds[decimalDigits], decimalDigits);
145 SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt
147 for (int i = 0; i < decimalDigits; i++, pt++) {
149 sb.appendC(s1.charAt(pt));
153 s1 = (isNeg ? "-" : "") + sb;
154 return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',
159 * an alternative to DecimalFormat "0.#"
163 * @return formatted number
165 public static String formatDecimalTrimmed(double x, int precision) {
166 String str = formatDecimalDbl(x, precision);
167 int m = str.length() - 1;
169 while (m >= 0 && str.charAt(m) == zero)
171 return str.substring(0, m + 1); // 0.##...