diff options
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java | 20 | ||||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java | 50 |
2 files changed, 55 insertions, 15 deletions
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 2258591..34d750a 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/FormatUtil.java @@ -936,7 +936,6 @@ public class FormatUtil private static Fmt parseCustomTextFormat(ExprBuf buf, Args args) { Fmt fmt = null; - Fmt emptyFmt = null; List<BiConsumer<StringBuilder,CharSource>> subFmts = new ArrayList<>(); int numPlaceholders = 0; @@ -1037,11 +1036,12 @@ public class FormatUtil flushPendingTextLiteral(pendingLiteral, subFmts); + Fmt emptyFmt = null; if(fmt == null) { fmt = new CharSourceFmt(subFmts, numPlaceholders, rightAligned, textCase); emptyFmt = NULL_FMT; - } else if(emptyFmt == null) { + } else { emptyFmt = (hasFmtChars ? new CharSourceFmt(subFmts, numPlaceholders, rightAligned, textCase) : @@ -1438,10 +1438,18 @@ public class FormatUtil private Value formatMaybeZero(BigDecimal bd, NumberFormat fmt) { // in theory we want to use the given format. however, if, due to // rounding, we end up with a number equivalent to zero, then we fall - // back to the zero format - int maxDecDigits = fmt.getMaximumFractionDigits(); - if(maxDecDigits < bd.scale()) { - bd = bd.setScale(maxDecDigits, NumberFormatter.ROUND_MODE); + // back to the zero format. if we are using scientific notation, + // however, then don't worry about this + if(!(fmt instanceof NumberFormatter.ScientificFormat)) { + int maxDecDigits = fmt.getMaximumFractionDigits(); + int mult = ((DecimalFormat)fmt).getMultiplier(); + while(mult > 1) { + ++maxDecDigits; + mult /= 10; + } + if(maxDecDigits < bd.scale()) { + bd = bd.setScale(maxDecDigits, NumberFormatter.ROUND_MODE); + } } if(BigDecimal.ZERO.compareTo(bd) == 0) { // fall back to zero format 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 24f3c5b..7275fb2 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -347,6 +347,8 @@ public class DefaultFunctionsTest extends TestCase "=Format('3.9', '*~dddd, yy mmm d, hh:nn:ss \\Y[Yellow]')"); assertEval("Tuesday, 00 Jan 01/2, 09:36:00 PM", "=Format('3.9', 'dddd, yy mmm mm/d, hh:nn:ss AMPM')"); + assertEval("9:36:00 PM", + "=Format('3.9', 'ttttt')"); assertEval("foo", "=Format('foo', 'dddd, yy mmm mm d, hh:nn:ss AMPM')"); @@ -390,24 +392,46 @@ public class DefaultFunctionsTest extends TestCase "", "''", "", "Null"); - assertEvalFormat("'\"p\"#.00#\"blah\";(\"p\"#.00#\"blah\");\"zero\";\"yuck\"'", + assertEvalFormat("'\\p#.00#\"blah\";*~(\"p\"#.00#\"blah\");\"zero\";\"yuck\"'", "p13.00blah", "13", "(p13.00blah)", "-13", "zero", "0", "", "''", "yuck", "Null"); - assertEvalFormat("'0.##;(0.###);\"zero\";\"yuck\"'", + assertEvalFormat("'0.##;(0.###);\"zero\";\"yuck\";'", "0.03", "0.03", "zero", "0.003", "(0.003)", "-0.003", "zero", "-0.0003"); - // FIXME, need to handle rounding w/ negatives - // FIXME, need to handle dangling decimal - // assertEvalFormat("'0.##;(0.###E+0)'", - // "0.03", "0.03", - // "(0.003)", "-0.0003", + assertEvalFormat("'0.##;(0.###E+0)'", + "0.03", "0.03", + "(3.E-4)", "-0.0003", + "0.", "0", + "34223.", "34223", + "(3.422E+4)", "-34223"); + + assertEvalFormat("'0.###E-0'", + "3.E-4", "0.0003", + "3.422E4", "34223" + ); + + assertEvalFormat("'0.###e+0'", + "3.e-4", "0.0003", + "3.422e+4", "34223" + ); + + assertEvalFormat("'0.###e-0'", + "3.e-4", "0.0003", + "3.422e4", "34223" + ); + + assertEvalFormat("'#,##0.###'", + "0.003", "0.003", + "0.", "0.0003", + "34,223.", "34223" + ); assertEvalFormat("'0.'", "13.", "13", @@ -434,6 +458,14 @@ public class DefaultFunctionsTest extends TestCase "0", "0" ); + assertEvalFormat("'%0'", + "%13", "0.13", + "%0", "0.003", + "-%45", "-0.45", + "%0", "-0.003", + "%0", "0" + ); + assertEvalFormat("'#'", "13", "13", "0", "0.003", @@ -467,14 +499,14 @@ public class DefaultFunctionsTest extends TestCase "", "''", "", "Null"); - assertEvalFormat("'!>@'", + assertEvalFormat("'!>@;'", "O", "'foo'", "3", "-13", "0", "0", "", "''", "", "Null"); - assertEvalFormat("'!>@[Red];\"empty\"'", + assertEvalFormat("'!>*~@[Red];\"empty\";'", "O", "'foo'", "3", "-13", "0", "0", |