JAL-3026 adding javajs package
[jalview.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  * 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.
8  *
9  * Copyright (C) 2005  The Jmol Development Team
10  *
11  * Contact: jmol-developers@lists.sf.net
12  *
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.
17  *
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.
22  *
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.
26  */
27
28 package javajs.util;
29
30 /**
31  * created to remove ambiguities and make a simpler DecimalFormat
32  */
33 public class DF {
34
35   private final static String[] formattingStrings = { "0", "0.0", "0.00",
36       "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000",
37       "0.000000000" };
38   private final static String zeros = "0000000000000000000000000000000000000000";
39
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 };
42
43   private final static Boolean[] useNumberLocalization = new Boolean[] { Boolean.TRUE };
44
45   public static void setUseNumberLocalization(boolean TF) {
46     useNumberLocalization[0] = (TF ? Boolean.TRUE : Boolean.FALSE);
47   }
48
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))
54       return "" + value;
55     return DF.formatDecimal((float) value, decimalDigits);
56   }
57
58   /**
59    * a simple alternative to DecimalFormat (which Java2Script does not have
60    * and which is quite too complex for our use here.)
61    * 
62    * @param value
63    * @param decimalDigits
64    * @return  formatted decimal
65    */
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))
69       return "" + value;
70     int n;
71     if (decimalDigits < 0) {
72       decimalDigits = -decimalDigits;
73       if (decimalDigits > formattingStrings.length)
74         decimalDigits = formattingStrings.length;
75       if (value == 0)
76         return formattingStrings[decimalDigits] + "E+0";
77       //scientific notation
78       n = 0;
79       double d;
80       if (Math.abs(value) < 1) {
81         n = 10;
82         d = value * 1e-10;
83       } else {
84         n = -10;
85         d = value * 1e10;
86       }
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);
93     }
94   
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("-");
102     if (isNeg) {
103       s1 = s1.substring(1);
104       pt--;
105     }
106     int pt1 = s1.indexOf("E-");
107     if (pt1 > 0) {
108       n = PT.parseInt(s1.substring(pt1 + 1));
109       // 3.567E-2
110       // 0.03567
111       s1 = "0." + zeros.substring(0, -n - 1) + s1.substring(0, 1) + s1.substring(2, pt1);
112       pt = 1; 
113     }
114   
115     pt1 = s1.indexOf("E");
116     // 3.5678E+3
117     // 3567.800000000
118     // 1.234E10 %3.8f -> 12340000000.00000000
119     if (pt1 > 0) {
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(".");
124     } 
125     // "234.345667  len == 10; pt = 3
126     // "  0.0 "  decimalDigits = 1
127     
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);
133     }
134   
135     SB sb = SB.newS(s1.substring(0, (decimalDigits == 0 ? pt
136         : ++pt)));
137     for (int i = 0; i < decimalDigits; i++, pt++) {
138       if (pt < len)
139         sb.appendC(s1.charAt(pt));
140       else
141         sb.appendC('0');
142     }
143     s1 = (isNeg ? "-" : "") + sb;
144     return (Boolean.TRUE.equals(useNumberLocalization[0]) ? s1 : s1.replace(',',
145         '.'));
146   }
147
148   /**
149    * an alternative to DecimalFormat "0.#"
150    * 
151    * @param x
152    * @param precision
153    * @return  formatted number 
154    */
155   public static String formatDecimalTrimmed(double x, int precision) {
156     String str = formatDecimalDbl(x, precision);
157     int m = str.length() - 1;
158     char zero = '0';
159     while (m >= 0 && str.charAt(m) == zero)
160       m--;
161     return str.substring(0, m + 1); // 0.##...
162   }
163
164 }