aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2018-11-07 21:11:58 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2018-11-07 21:11:58 +0000
commit47de466737598cfdf8d8ec7bc11f624df3fed283 (patch)
tree2724beecfdae3b6b588719e5c1f0503532d21c55
parentb1fa4abea50fa89f8515041009d5b79bd44e6f2b (diff)
downloadjackcess-47de466737598cfdf8d8ec7bc11f624df3fed283.tar.gz
jackcess-47de466737598cfdf8d8ec7bc11f624df3fed283.zip
untested support for FormatNumber
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1217 f203690c-595d-4dc9-a70b-905162fa7fd2
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java
index 24f6d99..687edf9 100644
--- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java
+++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java
@@ -18,6 +18,7 @@ package com.healthmarketscience.jackcess.impl.expr;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
@@ -278,6 +279,42 @@ public class DefaultFunctions
}
});
+ public static final Function FORMATNUMBER = registerFunc(new FuncVar("FormatNumber", 1, 6) {
+ @Override
+ protected Value evalVar(EvalContext ctx, Value[] params) {
+ Value param1 = params[0];
+ if(param1.isNull()) {
+ return ValueSupport.NULL_VAL;
+ }
+
+ int numDecDigits = getOptionalIntParam(ctx, params, 1, 2, -1);
+ boolean incLeadDigit = getOptionalTriStateBoolean(ctx, params, 2, true);
+ boolean negParens = getOptionalTriStateBoolean(ctx, params, 3, false);
+ boolean groupDigits = getOptionalTriStateBoolean(ctx, params, 4, true);
+
+ StringBuilder fmt = new StringBuilder();
+
+ fmt.append(incLeadDigit ? "0" : "#");
+ if(numDecDigits > 0) {
+ fmt.append(".");
+ for(int i = 0; i < numDecDigits; ++i) {
+ fmt.append("0");
+ }
+ }
+
+ if(negParens) {
+ fmt.append(";(#)");
+ }
+
+ // Note, DecimalFormat rounding mode uses HALF_EVEN by default
+ DecimalFormat df = new DecimalFormat(
+ fmt.toString(), ctx.getNumericConfig().getDecimalFormatSymbols());
+ df.setGroupingUsed(groupDigits);
+
+ return ValueSupport.toValue(df.format(param1.getAsBigDecimal(ctx)));
+ }
+ });
+
public static final Function VARTYPE = registerFunc(new Func1("VarType") {
@Override
protected Value eval1(EvalContext ctx, Value param1) {
@@ -417,6 +454,31 @@ public class DefaultFunctions
return false;
}
+ private static boolean getOptionalTriStateBoolean(
+ EvalContext ctx, Value[] params, int idx, boolean defValue) {
+ boolean bv = defValue;
+ if(params.length > idx) {
+ int val = params[idx].getAsLongInt(ctx);
+ switch(val) {
+ case 0:
+ // vbFalse
+ bv = false;
+ break;
+ case -1:
+ // vbTrue
+ bv = true;
+ break;
+ case -2:
+ // vbUseDefault
+ bv = defValue;
+ break;
+ default:
+ throw new EvalException("Unsupported tri-state boolean value " + val);
+ }
+ }
+ return bv;
+ }
+
// https://www.techonthenet.com/access/functions/
// https://support.office.com/en-us/article/Access-Functions-by-category-b8b136c3-2716-4d39-94a2-658ce330ed83