From: James Ahlborn Date: Thu, 15 Nov 2018 05:22:13 +0000 (+0000) Subject: currency format uses parens; add bool predefined formats X-Git-Tag: jackcess-2.2.1~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8c64c96af69182bfb64f98cebebfe73e3b1c111d;p=jackcess.git currency format uses parens; add bool predefined formats git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1225 f203690c-595d-4dc9-a70b-905162fa7fd2 --- diff --git a/src/main/java/com/healthmarketscience/jackcess/expr/NumericConfig.java b/src/main/java/com/healthmarketscience/jackcess/expr/NumericConfig.java index f8fd51b..cf9bacb 100644 --- a/src/main/java/com/healthmarketscience/jackcess/expr/NumericConfig.java +++ b/src/main/java/com/healthmarketscience/jackcess/expr/NumericConfig.java @@ -31,19 +31,22 @@ import java.util.Locale; public class NumericConfig { public static final NumericConfig US_NUMERIC_CONFIG = new NumericConfig( - 2, true, false, 3, Locale.US); + 2, true, false, true, 3, Locale.US); private final int _numDecDigits; private final boolean _incLeadingDigit; private final boolean _useNegParens; + private final boolean _useNegCurrencyParens; private final int _numGroupDigits; private final DecimalFormatSymbols _symbols; public NumericConfig(int numDecDigits, boolean incLeadingDigit, - boolean useNegParens, int numGroupDigits, Locale locale) { + boolean useNegParens, boolean useNegCurrencyParens, + int numGroupDigits, Locale locale) { _numDecDigits = numDecDigits; _incLeadingDigit = incLeadingDigit; _useNegParens = useNegParens; + _useNegCurrencyParens = useNegCurrencyParens; _numGroupDigits = numGroupDigits; _symbols = DecimalFormatSymbols.getInstance(locale); } @@ -60,6 +63,10 @@ public class NumericConfig return _useNegParens; } + public boolean useParensForCurrencyNegatives() { + return _useNegCurrencyParens; + } + public int getNumGroupingDigits() { return _numGroupDigits; } 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 dcb2348..d663309 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java @@ -537,8 +537,10 @@ public class DefaultFunctions ctx, params, 1, cfg.getNumDecimalDigits(), -1); boolean incLeadDigit = getOptionalTriStateBoolean( ctx, params, 2, cfg.includeLeadingDigit()); + boolean defNegParens = (isCurrency ? cfg.useParensForCurrencyNegatives() : + cfg.useParensForNegatives()); boolean negParens = getOptionalTriStateBoolean( - ctx, params, 3, cfg.useParensForNegatives()); + ctx, params, 3, defNegParens); int numGroupDigits = cfg.getNumGroupingDigits(); boolean groupDigits = getOptionalTriStateBoolean( ctx, params, 4, (numGroupDigits > 0)); @@ -553,7 +555,7 @@ public class DefaultFunctions fmt.append("#,"); DefaultTextFunctions.nchars(fmt, numGroupDigits - 1, '#'); } - + fmt.append(incLeadDigit ? "0" : "#"); if(numDecDigits > 0) { fmt.append("."); diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java index 730961d..1d9e21e 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java @@ -28,7 +28,7 @@ import com.healthmarketscience.jackcess.expr.Value; * * @author James Ahlborn */ -public class FormatUtil +public class FormatUtil { private static final Map PREDEF_FMTS = new HashMap(); @@ -48,8 +48,11 @@ public class FormatUtil PREDEF_FMTS.put("Short Time", new PredefDateFmt(TemporalConfig.Type.SHORT_TIME)); + PREDEF_FMTS.put("True/False", new PredefBoolFmt("True", "False")); + PREDEF_FMTS.put("Yes/No", new PredefBoolFmt("Yes", "No")); + PREDEF_FMTS.put("On/Off", new PredefBoolFmt("On", "Off")); } - + private FormatUtil() {} @@ -75,7 +78,7 @@ public class FormatUtil private PredefDateFmt(TemporalConfig.Type type) { _type = type; } - + @Override public Value format(EvalContext ctx, Value expr, String fmtStr, int firstDay, int firstWeekType) { @@ -94,7 +97,7 @@ public class FormatUtil _trueVal = ValueSupport.toValue(trueStr); _falseVal = ValueSupport.toValue(falseStr); } - + @Override public Value format(EvalContext ctx, Value expr, String fmtStr, int firstDay, int firstWeekType) { @@ -103,5 +106,5 @@ public class FormatUtil } } - + } diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java index fdab104..8c015d8 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -250,9 +250,9 @@ public class DefaultFunctionsTest extends TestCase assertEval("-.123%", "=FormatPercent(-0.0012345,3,False)"); assertEval("$12,345.00", "=FormatCurrency(12345)"); - assertEval("($12.34)", "=FormatCurrency(-12.345,-1,True,True)"); + assertEval("-$12.34", "=FormatCurrency(-12.345,-1,True,False)"); assertEval("$12", "=FormatCurrency(12.345,0,True,True)"); - assertEval("-$.123", "=FormatCurrency(-0.12345,3,False)"); + assertEval("($.123)", "=FormatCurrency(-0.12345,3,False)"); assertEval("1/1/1973 1:37:25 PM", "=FormatDateTime(#1/1/1973 1:37:25 PM#)"); assertEval("1:37:25 PM", "=FormatDateTime(#1:37:25 PM#,0)");