| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.util; |
| 27 |
|
| 28 |
import java.io.Serializable; |
| 29 |
import java.text.DecimalFormat; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 103 |
|
| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
} |