package com.healthmarketscience.jackcess.expr;
+import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
* {@link EvalConfig})
*/
public NumericConfig getNumericConfig();
+
+ /**
+ * @return an appropriately configured (i.e. DecimalFormatSymbols)
+ * DecimalFormat for the given format.
+ */
+ public DecimalFormat createDecimalFormat(String formatStr);
}
import java.io.IOException;
import java.math.BigDecimal;
+import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
return _dbCtx.getNumericConfig();
}
+ public DecimalFormat createDecimalFormat(String formatStr) {
+ return _dbCtx.createDecimalFormat(formatStr);
+ }
+
public float getRandom(Integer seed) {
return _dbCtx.getRandom(seed);
}
package com.healthmarketscience.jackcess.impl;
+import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
private final DatabaseImpl _db;
private FunctionLookup _funcs = DefaultFunctions.LOOKUP;
private Map<String,SimpleDateFormat> _sdfs;
+ private Map<String,DecimalFormat> _dfs;
private TemporalConfig _temporal = TemporalConfig.US_TEMPORAL_CONFIG;
private NumericConfig _numeric = NumericConfig.US_NUMERIC_CONFIG;
private final RandomContext _rndCtx = new RandomContext();
return sdf;
}
+ public DecimalFormat createDecimalFormat(String formatStr) {
+ if(_dfs == null) {
+ _dfs = new SimpleCache<String,DecimalFormat>(MAX_CACHE_SIZE);
+ }
+ DecimalFormat df = _dfs.get(formatStr);
+ if(df == null) {
+ df = new DecimalFormat(formatStr, _numeric.getDecimalFormatSymbols());
+ _dfs.put(formatStr, df);
+ }
+ return df;
+ }
+
public float getRandom(Integer seed) {
return _rndCtx.getRandom(seed);
}
fmt.append("\u00A4");
}
+ if(groupDigits) {
+ fmt.append("#,");
+ appendNum(fmt, '#', numGroupDigits - 1);
+ }
+
fmt.append(incLeadDigit ? "0" : "#");
if(numDecDigits > 0) {
fmt.append(".");
- for(int i = 0; i < numDecDigits; ++i) {
- fmt.append("0");
- }
+ appendNum(fmt, '0', numDecDigits);
}
if(isPercent) {
}
// Note, DecimalFormat rounding mode uses HALF_EVEN by default
- DecimalFormat df = new DecimalFormat(
- fmt.toString(), cfg.getDecimalFormatSymbols());
- if(groupDigits) {
- df.setGroupingUsed(true);
- df.setGroupingSize(numGroupDigits);
- } else {
- df.setGroupingUsed(false);
- df.setGroupingSize(numGroupDigits);
- }
+ DecimalFormat df = ctx.createDecimalFormat(fmt.toString());
return ValueSupport.toValue(df.format(param1.getAsBigDecimal(ctx)));
}
throw new IllegalStateException("Duplicate function " + fname);
}
}
+
+ private static void appendNum(StringBuilder sb, char c, int num) {
+ for(int i = 0; i < num; ++i) {
+ sb.append(c);
+ }
+ }
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigDecimal;
+import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
return NumericConfig.US_NUMERIC_CONFIG;
}
+ public DecimalFormat createDecimalFormat(String formatStr) {
+ return new DecimalFormat(
+ formatStr, NumericConfig.US_NUMERIC_CONFIG.getDecimalFormatSymbols());
+ }
+
public FunctionLookup getFunctionLookup() {
return DefaultFunctions.LOOKUP;
}