Debian Codegraph @

src/dr/util/NumberFormatter.java

raw symbols SCIP
1 /*
2 * NumberFormatter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST 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
18 * GNU 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 BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.util;
27
28 import java.io.Serializable;
29 import java.text.DecimalFormat;
30
31 /**
32 * The world's most intelligent number formatter with the following features :-)
33 * <P>
34 * It guarantee's the display of a user-specified number of significant figures, sf <BR>
35 * It displays decimal format for numbers with absolute values between 1 and 10^(sf-1) <BR>
36 * It displays scientific notation for all other numbers (i.e. really big and really small absolute values) <BR>
37 * <b>note</b>: Its display integers for doubles with integer value <BR>
38 *
39 * @version $Id: NumberFormatter.java,v 1.6 2005/05/24 20:26:01 rambaut Exp $
40 *
41 * @author Alexei Drummond
42 */
43 public class NumberFormatter implements Serializable {
44
45 private int sf;
46 private double upperCutoff;
47 private double[] cutoffTable;
48 private final DecimalFormat decimalFormat = new DecimalFormat();
49 private DecimalFormat scientificFormat = null;
50 private boolean isPadding = false;
51 private int fieldWidth;
52
53
54 public NumberFormatter(int sf) {
55 setSignificantFigures(sf);
56 }
57
58 public NumberFormatter(int sf, int fieldWidth) {
59 setSignificantFigures(sf);
60 setPadding(true);
61 setFieldWidth(fieldWidth);
62 }
63
64 public void setSignificantFigures(int sf) {
65 this.sf = sf;
66 upperCutoff = Math.pow(10, Math.max(sf-1, 0));
67 cutoffTable = new double[sf];
68 long num = 10;
69 for (int i =0; i < cutoffTable.length; i++) {
70 cutoffTable[i] = (double)num;
71 num *= 10;
72 }
73 decimalFormat.setMinimumIntegerDigits(1);
74 decimalFormat.setMaximumFractionDigits(Math.max(sf-1, 0));
75 decimalFormat.setMinimumFractionDigits(Math.max(sf-1, 0));
76 decimalFormat.setGroupingUsed(false);
77 scientificFormat = new DecimalFormat(getScientificPattern(sf));
78 fieldWidth = sf;
79 }
80
81 public void setPadding(boolean padding) {
82 isPadding = padding;
83 }
84
85 public void setFieldWidth(int fw) {
86 if (fw < sf+4) throw new IllegalArgumentException();
87 fieldWidth = fw;
88 }
89
90 public int getFieldWidth() { return fieldWidth; }
91
92 public String formatToFieldWidth(String s, int fieldWidth) {
93 int size = fieldWidth - s.length();
94 StringBuffer buffer = new StringBuffer(s);
95 for (int i =0; i < size; i++) {
96 buffer.append(' ');
97 }
98 return buffer.toString();
99 }
100
101 /**
102 * @return the given value formatted to have exactly then number of
103 * fraction digits specified.
104 */
105 public String formatDecimal(double value, int numFractionDigits) {
106
107 decimalFormat.setMaximumFractionDigits(numFractionDigits);
108 decimalFormat.setMinimumFractionDigits(Math.min(numFractionDigits, 1));
109 return decimalFormat.format(value);
110 }
111
112 /**
113 * This method formats a number 'nicely': <BR>
114 * It guarantee's the display of a user-specified total significant figures, sf <BR>
115 * It displays decimal format for numbers with absolute values between 1 and 10^(sf-1) <BR>
116 * It displays scientific notation for all other numbers (i.e. really big and really small absolute values) <BR>
117 * <b>note</b>: Its display integers for doubles with integer value <BR>
118 * @return a nicely formatted number.
119 */
120 public String format(double value) {
121
122 StringBuffer buffer = new StringBuffer();
123
124 double absValue = Math.abs(value);
125
126 if (sf > 0 && (absValue > upperCutoff) || (absValue < (1.0 / upperCutoff) && absValue != 0.0)) {
127 buffer.append(scientificFormat.format(value));
128 } else {
129 int numFractionDigits = 0;
130 if (value != (int)value) {
131 numFractionDigits = getNumFractionDigits(value);
132 }
133 buffer.append(formatDecimal(value, numFractionDigits));
134 }
135
136 if (isPadding) {
137 int size = fieldWidth - buffer.length();
138 for (int i =0; i < size; i++) {
139 buffer.append(' ');
140 }
141 }
142 return buffer.toString();
143 }
144
145 private int getNumFractionDigits(double value) {
146 value = Math.abs(value);
147 for (int i =0; i < cutoffTable.length; i++) {
148 if (value < cutoffTable[i]) return sf-i-1;
149 }
150 return sf - 1;
151 }
152
153 private String getScientificPattern(int sf) {
154 String pattern = "0.";
155 for (int i =0; i < sf-1; i++) {
156 pattern += "#";
157 }
158 pattern += "E0";
159 return pattern;
160 }
161 }