-/* $RCSfile$\r
- * $Author: hansonr $\r
- * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $\r
- * $Revision: 7502 $\r
- *\r
- * Copyright (C) 2005 The Jmol Development Team\r
- *\r
- * Contact: jmol-developers@lists.sf.net\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Lesser General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2.1 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- * Lesser General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Lesser General Public\r
- * License along with this library; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
- */\r
-\r
-package javajs.util;\r
-\r
-/**\r
- * created to remove ambiguities and make a simpler DecimalFormat\r
- */\r
-public class DF {\r
-\r
- private final static String[] formattingStrings = { "0", "0.0", "0.00",\r
- "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000",\r
- "0.000000000" };\r
- private final static String zeros = "0000000000000000000000000000000000000000";\r
-\r
- private final static float[] formatAdds = { 0.5f, 0.05f, 0.005f, 0.0005f,\r
- 0.00005f, 0.000005f, 0.0000005f, 0.00000005f, 0.000000005f, 0.0000000005f };\r
-\r
- private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE };\r
-\r
- public static void setUseNumberLocalization(boolean TF) {\r
- useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE);\r
- }\r
-\r
- public static String formatDecimalDbl(double value, int decimalDigits) {\r
- if (decimalDigits == Integer.MAX_VALUE \r
- || value == Double.NEGATIVE_INFINITY\r
- || value == Double.POSITIVE_INFINITY \r
- || Double.isNaN(value))\r
- return "" + value;\r
- return DF.formatDecimal((float) value, decimalDigits);\r
- }\r
-\r
- /**\r
- * a simple alternative to DecimalFormat (which Java2Script does not have\r
- * and which is quite too complex for our use here.)\r
- * \r
- * @param value\r
- * @param decimalDigits\r
- * @return formatted decimal\r
- */\r
- public static String formatDecimal(float value, int decimalDigits) {\r
- if (decimalDigits == Integer.MAX_VALUE \r
- || value == Float.NEGATIVE_INFINITY || value == Float.POSITIVE_INFINITY || Float.isNaN(value))\r
- return "" + value;\r
- int n;\r
- if (decimalDigits < 0) {\r
- decimalDigits = -decimalDigits;\r
- if (decimalDigits > formattingStrings.length)\r
- decimalDigits = formattingStrings.length;\r
- if (value == 0)\r
- return formattingStrings[decimalDigits] + "E+0";\r
- //scientific notation\r
- n = 0;\r
- double d;\r
- if (Math.abs(value) < 1) {\r
- n = 10;\r
- d = value * 1e-10;\r
- } else {\r
- n = -10;\r
- d = value * 1e10;\r
- }\r
- String s = ("" + d).toUpperCase();\r
- int i = s.indexOf("E");\r
- n = PT.parseInt(s.substring(i + 1)) + n;\r
- return (i < 0 ? "" + value : formatDecimal(PT.parseFloat(s.substring(\r
- 0, i)), decimalDigits - 1)\r
- + "E" + (n >= 0 ? "+" : "") + n);\r
- }\r
- \r
- if (decimalDigits >= formattingStrings.length)\r
- decimalDigits = formattingStrings.length - 1;\r
- String s1 = ("" + value).toUpperCase();\r
- int pt = s1.indexOf(".");\r
- if (pt < 0) // specifically JavaScript "-2" not "-2.0"\r
- return s1 + formattingStrings[decimalDigits].substring(1);\r
- boolean isNeg = s1.startsWith("-");\r
- if (isNeg) {\r
- s1 = s1.substring(1);\r
- pt--;\r
- }\r
- int pt1 = s1.indexOf("E-");\r
- if (pt1 > 0) {\r
- n = PT.parseInt(s1.substring(pt1 + 1));\r
- // 3.567E-2\r
- // 0.03567\r
- s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);\r
- pt = 1; \r
- }\r
- \r
- pt1 = s1.indexOf("E");\r
- // 3.5678E+3\r
- // 3567.800000000\r
- // 1.234E10 %3.8f -> 12340000000.00000000\r
- if (pt1 > 0) {\r
- n = PT.parseInt(s1.substring(pt1 + 1));\r
- s1 = s1.substring(0, 1) + s1.substring(2, pt1) + zeros;\r
- s1 = s1.substring(0, n + 1) + "." + s1.substring(n + 1);\r
- pt = s1.indexOf(".");\r
- } \r
- // "234.345667 len == 10; pt = 3\r
- // " 0.0 " decimalDigits = 1\r
- \r
- int len = s1.length();\r
- int pt2 = decimalDigits + pt + 1;\r
- if (pt2 < len && s1.charAt(pt2) >= '5') {\r
- return formatDecimal(\r
- value + (isNeg ? -1 : 1) * formatAdds[decimalDigits], decimalDigits);\r
- }\r
- \r
- SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt\r
- : ++pt)));\r
- for (int i = 0; i < decimalDigits; i++, pt++) {\r
- if (pt < len)\r
- sb.appendC(s1.charAt(pt));\r
- else\r
- sb.appendC('0');\r
- }\r
- s1 = (isNeg ? "-" : "") + sb;\r
- return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',\r
- '.'));\r
- }\r
-\r
- /**\r
- * an alternative to DecimalFormat "0.#"\r
- * \r
- * @param x\r
- * @param precision\r
- * @return formatted number \r
- */\r
- public static String formatDecimalTrimmed(double x, int precision) {\r
- String str = formatDecimalDbl(x, precision);\r
- int m = str.length() - 1;\r
- char zero = '0';\r
- while (m >= 0 && str.charAt(m) == zero)\r
- m--;\r
- return str.substring(0, m + 1); // 0.##...\r
- }\r
-\r
-}\r
+/* $RCSfile$
+ * $Author: hansonr $
+ * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $
+ * $Revision: 7502 $
+ *
+ * Copyright (C) 2005 The Jmol Development Team
+ *
+ * Contact: jmol-developers@lists.sf.net
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+package javajs.util;
+
+/**
+ * created to remove ambiguities and make a simpler DecimalFormat
+ */
+public class DF {
+
+ private final static String[] formattingStrings = { "0", "0.0", "0.00",
+ "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000",
+ "0.000000000" };
+ private final static String zeros = "0000000000000000000000000000000000000000";
+
+ private final static float[] formatAdds = { 0.5f, 0.05f, 0.005f, 0.0005f,
+ 0.00005f, 0.000005f, 0.0000005f, 0.00000005f, 0.000000005f, 0.0000000005f };
+
+ private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE };
+
+ public static void setUseNumberLocalization(boolean TF) {
+ useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE);
+ }
+
+ public static String formatDecimalDbl(double value, int decimalDigits) {
+ if (decimalDigits == Integer.MAX_VALUE
+ || value == Double.NEGATIVE_INFINITY
+ || value == Double.POSITIVE_INFINITY
+ || Double.isNaN(value))
+ return "" + value;
+ return DF.formatDecimal((float) value, decimalDigits);
+ }
+
+ /**
+ * a simple alternative to DecimalFormat (which Java2Script does not have
+ * and which is quite too complex for our use here.)
+ *
+ * @param value
+ * @param decimalDigits
+ * @return formatted decimal
+ */
+ public static String formatDecimal(float value, int decimalDigits) {
+ if (decimalDigits == Integer.MAX_VALUE
+ || value == Float.NEGATIVE_INFINITY || value == Float.POSITIVE_INFINITY || Float.isNaN(value))
+ return "" + value;
+ int n;
+ if (decimalDigits < 0) {
+ decimalDigits = -decimalDigits;
+ if (decimalDigits > formattingStrings.length)
+ decimalDigits = formattingStrings.length;
+ if (value == 0)
+ return formattingStrings[decimalDigits] + "E+0";
+ //scientific notation
+ n = 0;
+ double d;
+ if (Math.abs(value) < 1) {
+ n = 10;
+ d = value * 1e-10;
+ } else {
+ n = -10;
+ d = value * 1e10;
+ }
+ String s = ("" + d).toUpperCase();
+ int i = s.indexOf("E");
+ n = PT.parseInt(s.substring(i + 1)) + n;
+ return (i < 0 ? "" + value : formatDecimal(PT.parseFloat(s.substring(
+ 0, i)), decimalDigits - 1)
+ + "E" + (n >= 0 ? "+" : "") + n);
+ }
+
+ if (decimalDigits >= formattingStrings.length)
+ decimalDigits = formattingStrings.length - 1;
+ String s1 = ("" + value).toUpperCase();
+ int pt = s1.indexOf(".");
+ if (pt < 0) // specifically JavaScript "-2" not "-2.0"
+ return s1 + formattingStrings[decimalDigits].substring(1);
+ boolean isNeg = s1.startsWith("-");
+ if (isNeg) {
+ s1 = s1.substring(1);
+ pt--;
+ }
+ int pt1 = s1.indexOf("E-");
+ if (pt1 > 0) {
+ n = PT.parseInt(s1.substring(pt1 + 1));
+ // 3.567E-2
+ // 0.03567
+ s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);
+ pt = 1;
+ }
+
+ pt1 = s1.indexOf("E");
+ // 3.5678E+3
+ // 3567.800000000
+ // 1.234E10 %3.8f -> 12340000000.00000000
+ if (pt1 > 0) {
+ n = PT.parseInt(s1.substring(pt1 + 1));
+ s1 = s1.substring(0, 1) + s1.substring(2, pt1) + zeros;
+ s1 = s1.substring(0, n + 1) + "." + s1.substring(n + 1);
+ pt = s1.indexOf(".");
+ }
+ // "234.345667 len == 10; pt = 3
+ // " 0.0 " decimalDigits = 1
+
+ int len = s1.length();
+ int pt2 = decimalDigits + pt + 1;
+ if (pt2 < len && s1.charAt(pt2) >= '5') {
+ return formatDecimal(
+ value + (isNeg ? -1 : 1) * formatAdds[decimalDigits], decimalDigits);
+ }
+
+ SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt
+ : ++pt)));
+ for (int i = 0; i < decimalDigits; i++, pt++) {
+ if (pt < len)
+ sb.appendC(s1.charAt(pt));
+ else
+ sb.appendC('0');
+ }
+ s1 = (isNeg ? "-" : "") + sb;
+ return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',
+ '.'));
+ }
+
+ /**
+ * an alternative to DecimalFormat "0.#"
+ *
+ * @param x
+ * @param precision
+ * @return formatted number
+ */
+ public static String formatDecimalTrimmed(double x, int precision) {
+ String str = formatDecimalDbl(x, precision);
+ int m = str.length() - 1;
+ char zero = '0';
+ while (m >= 0 && str.charAt(m) == zero)
+ m--;
+ return str.substring(0, m + 1); // 0.##...
+ }
+
+}