bin/hex numbers should always use space as a grouping separator

This commit is contained in:
serso 2016-04-16 21:04:44 +02:00
parent ba7207d0e6
commit f7e2229687
2 changed files with 32 additions and 4 deletions

View File

@ -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,

View File

@ -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));