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] + "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;
90 return (i < 0 ? "" + value : formatDecimal(PT.parseFloat(s.substring(
91 0, i)), decimalDigits - 1)
92 + "E" + (n >= 0 ? "+" : "") + n);
95 if (decimalDigits >= formattingStrings.length)
96 decimalDigits = formattingStrings.length - 1;
97 String s1 = ("" + value).toUpperCase();
98 int pt = s1.indexOf(".");
99 if (pt < 0) // specifically JavaScript "-2" not "-2.0"
100 return s1 + formattingStrings[decimalDigits].substring(1);
101 boolean isNeg = s1.startsWith("-");
103 s1 = s1.substring(1);
106 int pt1 = s1.indexOf("E-");
108 n = PT.parseInt(s1.substring(pt1 + 1));
111 s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);
115 pt1 = s1.indexOf("E");
118 // 1.234E10 %3.8f -> 12340000000.00000000
120 n = PT.parseInt(s1.substring(pt1 + 1));
121 s1 = s1.substring(0, 1) + s1.substring(2, pt1) + zeros;
122 s1 = s1.substring(0, n + 1) + "." + s1.substring(n + 1);
123 pt = s1.indexOf(".");
125 // "234.345667 len == 10; pt = 3
126 // " 0.0 " decimalDigits = 1
128 int len = s1.length();
129 int pt2 = decimalDigits + pt + 1;
130 if (pt2 < len && s1.charAt(pt2) >= '5') {
131 return formatDecimal(
132 value + (isNeg ? -1 : 1) * formatAdds[decimalDigits], decimalDigits);
135 SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt
137 for (int i = 0; i < decimalDigits; i++, pt++) {
139 sb.appendC(s1.charAt(pt));
143 s1 = (isNeg ? "-" : "") + sb;
144 return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',
149 * an alternative to DecimalFormat "0.#"
153 * @return formatted number
155 public static String formatDecimalTrimmed(double x, int precision) {
156 String str = formatDecimalDbl(x, precision);
157 int m = str.length() - 1;
159 while (m >= 0 && str.charAt(m) == zero)
161 return str.substring(0, m + 1); // 0.##...