JAL-1807 update
[jalviewjs.git] / src / javajs / util / DF.java
1 /* $RCSfile$
2  * $Author: hansonr $
3  * $Date: 2007-04-26 16:57:51 -0500 (Thu, 26 Apr 2007) $
4  * $Revision: 7502 $
5  *
6  * Copyright (C) 2005  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 package javajs.util;
26
27 /**
28  * created to remove ambiguities and make a simpler DecimalFormat
29  */
30 public class DF {
31
32   private final static String[] formattingStrings = { "0", "0.0", "0.00",
33       "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000",
34       "0.000000000" };
35   private final static String zeros = "0000000000000000000000000000000000000000";
36
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 };
39
40   private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE };
41
42   public static void setUseNumberLocalization(boolean TF) {
43     useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE);
44   }
45
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))
51       return "" + value;
52     return DF.formatDecimal((float) value, decimalDigits);
53   }
54
55   /**
56    * a simple alternative to DecimalFormat (which Java2Script does not have
57    * and which is quite too complex for our use here.)
58    * 
59    * @param value
60    * @param decimalDigits
61    * @return  formatted decimal
62    */
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))
66       return "" + value;
67     int n;
68     if (decimalDigits < 0) {
69       decimalDigits = -decimalDigits;
70       if (decimalDigits > formattingStrings.length)
71         decimalDigits = formattingStrings.length;
72       if (value == 0)
73         return formattingStrings[decimalDigits] + "E+0";
74       //scientific notation
75       n = 0;
76       double d;
77       if (Math.abs(value) < 1) {
78         n = 10;
79         d = value * 1e-10;
80       } else {
81         n = -10;
82         d = value * 1e10;
83       }
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);
90     }
91   
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("-");
99     if (isNeg) {
100       s1 = s1.substring(1);
101       pt--;
102     }
103     int pt1 = s1.indexOf("E-");
104     if (pt1 > 0) {
105       n = PT.parseInt(s1.substring(pt1 + 1));
106       // 3.567E-2
107       // 0.03567
108       s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);
109       pt = 1; 
110     }
111   
112     pt1 = s1.indexOf("E");
113     // 3.5678E+3
114     // 3567.800000000
115     // 1.234E10 %3.8f -> 12340000000.00000000
116     if (pt1 > 0) {
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(".");
121     } 
122     // "234.345667  len == 10; pt = 3
123     // "  0.0 "  decimalDigits = 1
124     
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);
130     }
131   
132     SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt
133         : ++pt)));
134     for (int i = 0; i < decimalDigits; i++, pt++) {
135       if (pt < len)
136         sb.appendC(s1.charAt(pt));
137       else
138         sb.appendC('0');
139     }
140     s1 = (isNeg ? "-" : "") + sb;
141     return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',
142         '.'));
143   }
144
145   /**
146    * an alternative to DecimalFormat "0.#"
147    * 
148    * @param x
149    * @param precision
150    * @return  formatted number 
151    */
152   public static String formatDecimalTrimmed(double x, int precision) {
153     String str = formatDecimalDbl(x, precision);
154     int m = str.length() - 1;
155     char zero = '0';
156     while (m >= 0 && str.charAt(m) == zero)
157       m--;
158     return str.substring(0, m + 1); // 0.##...
159   }
160
161 }