From f7e2229687b278905c22ec378a98e05a2ddddf79 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 16 Apr 2016 21:04:44 +0200 Subject: [PATCH] bin/hex numbers should always use space as a grouping separator --- jscl/src/main/java/jscl/JsclMathEngine.java | 16 +++++++++++---- .../test/java/jscl/JsclMathEngineTest.java | 20 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/jscl/src/main/java/jscl/JsclMathEngine.java b/jscl/src/main/java/jscl/JsclMathEngine.java index f58333f0..8a59b2fe 100644 --- a/jscl/src/main/java/jscl/JsclMathEngine.java +++ b/jscl/src/main/java/jscl/JsclMathEngine.java @@ -9,7 +9,6 @@ import jscl.math.operator.Percent; import jscl.math.operator.Rand; import jscl.math.operator.matrix.OperatorsRegistry; import jscl.text.ParseException; -import midpcalc.Real; import org.solovyev.common.NumberFormatter; import org.solovyev.common.math.MathRegistry; import org.solovyev.common.msg.MessageRegistry; @@ -168,10 +167,10 @@ public class JsclMathEngine implements MathEngine { } } final NumberFormatter nf = numberFormatter.get(); - nf.setGroupingSeparator(useGroupingSeparator ? groupingSeparator : NumberFormatter.NO_GROUPING); + nf.setGroupingSeparator(useGroupingSeparator ? getGroupingSeparatorChar(nb) : NumberFormatter.NO_GROUPING); nf.setPrecision(roundResult ? precision : NumberFormatter.NO_ROUNDING); switch (numberFormat) { - case Real.NumberFormat.FSE_ENG: + case FSE_ENG: nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE); break; case FSE_SCI: @@ -251,7 +250,7 @@ public class JsclMathEngine implements MathEngine { @Nonnull public String addGroupingSeparators(@Nonnull NumeralBase nb, @Nonnull String ungroupedDoubleValue) { if (useGroupingSeparator) { - final String groupingSeparator = nb == NumeralBase.dec ? String.valueOf(this.groupingSeparator) : " "; + final String groupingSeparator = getGroupingSeparator(nb); final int dotIndex = ungroupedDoubleValue.indexOf("."); @@ -277,6 +276,15 @@ public class JsclMathEngine implements MathEngine { } } + @Nonnull + private String getGroupingSeparator(@Nonnull NumeralBase nb) { + return nb == NumeralBase.dec ? String.valueOf(groupingSeparator) : " "; + } + + private char getGroupingSeparatorChar(@Nonnull NumeralBase nb) { + return nb == NumeralBase.dec ? groupingSeparator : ' '; + } + @Nonnull private StringBuilder insertSeparators(@Nonnull NumeralBase nb, @Nonnull String groupingSeparator, diff --git a/jscl/src/test/java/jscl/JsclMathEngineTest.java b/jscl/src/test/java/jscl/JsclMathEngineTest.java index 96043c89..92b271fa 100644 --- a/jscl/src/test/java/jscl/JsclMathEngineTest.java +++ b/jscl/src/test/java/jscl/JsclMathEngineTest.java @@ -77,12 +77,31 @@ public class JsclMathEngineTest { assertEquals("13D", me.format(317d, NumeralBase.hex)); } + @Test public void testPiComputation() throws Exception { final JsclMathEngine me = JsclMathEngine.getInstance(); assertEquals("-1+0.0000000000000001*i", me.evaluate("exp(√(-1)*Π)")); } + @Test + public void testBinShouldAlwaysUseSpaceAsGroupingSeparator() throws Exception { + final JsclMathEngine me = new JsclMathEngine(); + me.setGroupingSeparator('\''); + me.setUseGroupingSeparator(true); + + assertEquals("100 0000 0000", me.format(1024d, NumeralBase.bin)); + } + + @Test + public void testHexShouldAlwaysUseSpaceAsGroupingSeparator() throws Exception { + final JsclMathEngine me = new JsclMathEngine(); + me.setGroupingSeparator('\''); + me.setUseGroupingSeparator(true); + + assertEquals("4 00", me.format(1024d, NumeralBase.hex)); + } + @Test public void testEngineeringNotationWithRounding() throws Exception { final JsclMathEngine me = JsclMathEngine.getInstance(); @@ -114,6 +133,7 @@ public class JsclMathEngineTest { assertEquals("-999", me.format(-999d)); assertEquals("-999.99", me.format(-999.99d)); assertEquals("-0.1", me.format(-0.1d)); + assertEquals("-0.12", me.format(-0.12d)); assertEquals("-0.123", me.format(-0.123d)); assertEquals("-0.1234", me.format(-0.1234d));