number formatting

This commit is contained in:
Sergey Solovyev 2011-12-15 12:45:15 +04:00
parent c1b83c5b99
commit e483ac6af5
5 changed files with 27 additions and 27 deletions

View File

@ -81,11 +81,6 @@ public enum CalculatorEngine {
this.engine.setUseGroupingSeparator(true); this.engine.setUseGroupingSeparator(true);
} }
@NotNull
public String format(@NotNull Double value, boolean round) {
return getEngine().format(value, round);
}
public static class Result { public static class Result {
@NotNull @NotNull

View File

@ -172,11 +172,11 @@ public class NumberBuilder {
indexOfE = number.indexOf('E'); indexOfE = number.indexOf('E');
} }
if (indexOfE < 0) { if (indexOfE < 0) {
formattedNumber = Numeric.toString(toDouble(number, nb), nb); formattedNumber = toString(toDouble(number, nb), nb);
} else { } else {
final String part; final String part;
if (indexOfDot != 0) { if (indexOfDot != 0) {
part = Numeric.toString(toDouble(number.substring(0, indexOfE), nb), nb); part = toString(toDouble(number.substring(0, indexOfE), nb), nb);
} else { } else {
part = ""; part = "";
} }
@ -185,14 +185,14 @@ public class NumberBuilder {
} else { } else {
final String integerPart; final String integerPart;
if (indexOfDot != 0) { if (indexOfDot != 0) {
integerPart = Numeric.toString(toDouble(number.substring(0, indexOfDot), nb), nb); integerPart = toString(toDouble(number.substring(0, indexOfDot), nb), nb);
} else { } else {
integerPart = ""; integerPart = "";
} }
formattedNumber = integerPart + number.substring(indexOfDot); formattedNumber = integerPart + number.substring(indexOfDot);
} }
} else { } else {
formattedNumber = Numeric.toString(toDouble(number, nb), nb); formattedNumber = toString(toDouble(number, nb), nb);
} }
if (numberOffset != null) { if (numberOffset != null) {
@ -205,6 +205,11 @@ public class NumberBuilder {
return result; return result;
} }
@NotNull
private static String toString(@NotNull Double value, @NotNull NumeralBase nb) {
return CalculatorEngine.instance.getEngine().format(value, nb);
}
public boolean isHexMode() { public boolean isHexMode() {
return nb == NumeralBase.hex || ( nb == null && defaultNumeralBase == NumeralBase.hex); return nb == NumeralBase.hex || ( nb == null && defaultNumeralBase == NumeralBase.hex);
} }

View File

@ -84,8 +84,8 @@ public class TextHighlighterTest {
Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:6F.B").toString()); Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:6F.B").toString());
Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:006F.B").toString()); Assert.assertEquals("<b>0x:</b>6F", textHighlighter.process("0x:006F.B").toString());
Assert.assertEquals("<b>0x:</b>0", textHighlighter.process("0x:0").toString()); Assert.assertEquals("<b>0x:</b>0", textHighlighter.process("0x:0").toString());
Assert.assertEquals("<b>0x:</b>FF33233FFE", textHighlighter.process("0x:FF33233FFE").toString()); Assert.assertEquals("<b>0x:</b>FF 33 23 3F FE", textHighlighter.process("0x:FF33233FFE").toString());
Assert.assertEquals("<b>0x:</b>FF33233FFE", textHighlighter.process("0x:FF33 233 FFE").toString()); Assert.assertEquals("<b>0x:</b>FF 33 23 3F FE", textHighlighter.process("0x:FF33 233 FFE").toString());
final MathEngine me = CalculatorEngine.instance.getEngine(); final MathEngine me = CalculatorEngine.instance.getEngine();
try { try {
@ -104,25 +104,25 @@ public class TextHighlighterTest {
me.setNumeralBase(NumeralBase.dec); me.setNumeralBase(NumeralBase.dec);
} }
Assert.assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101").toString()); Assert.assertEquals("<b>0b:</b>11 0101", textHighlighter.process("0b:110101").toString());
Assert.assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101.").toString()); Assert.assertEquals("<b>0b:</b>11 0101", textHighlighter.process("0b:110101.").toString());
Assert.assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101.101").toString()); Assert.assertEquals("<b>0b:</b>11 0101", textHighlighter.process("0b:110101.101").toString());
Assert.assertEquals("<b>0b:</b>11010100", textHighlighter.process("0b:11010100.1").toString()); Assert.assertEquals("<b>0b:</b>1101 0100", textHighlighter.process("0b:11010100.1").toString());
Assert.assertEquals("<b>0b:</b>110101", textHighlighter.process("0b:110101.0").toString()); Assert.assertEquals("<b>0b:</b>11 0101", textHighlighter.process("0b:110101.0").toString());
Assert.assertEquals("<b>0b:</b>0", textHighlighter.process("0b:0").toString()); Assert.assertEquals("<b>0b:</b>0", textHighlighter.process("0b:0").toString());
Assert.assertEquals("<b>0b:</b>1010100101111010101001", textHighlighter.process("0b:1010100101111010101001").toString()); Assert.assertEquals("<b>0b:</b>10 1010 0101 1110 1010 1001", textHighlighter.process("0b:1010100101111010101001").toString());
Assert.assertEquals("<b>0b:</b>1010100101111010101001", textHighlighter.process("0b:101 010 01 0 111 1 0 10101001").toString()); Assert.assertEquals("<b>0b:</b>10 1010 0101 1110 1010 1001", textHighlighter.process("0b:101 010 01 0 111 1 0 10101001").toString());
try { try {
me.setNumeralBase(NumeralBase.bin); me.setNumeralBase(NumeralBase.bin);
Assert.assertEquals("110101", textHighlighter.process("110101").toString()); Assert.assertEquals("11 0101", textHighlighter.process("110101").toString());
Assert.assertEquals("110101", textHighlighter.process("110101.").toString()); Assert.assertEquals("11 0101", textHighlighter.process("110101.").toString());
Assert.assertEquals("110101", textHighlighter.process("110101.101").toString()); Assert.assertEquals("11 0101", textHighlighter.process("110101.101").toString());
Assert.assertEquals("11010100", textHighlighter.process("11010100.1").toString()); Assert.assertEquals("1101 0100", textHighlighter.process("11010100.1").toString());
Assert.assertEquals("110101", textHighlighter.process("110101.0").toString()); Assert.assertEquals("11 0101", textHighlighter.process("110101.0").toString());
Assert.assertEquals("0", textHighlighter.process("0").toString()); Assert.assertEquals("0", textHighlighter.process("0").toString());
Assert.assertEquals("1010100101111010101001", textHighlighter.process("1010100101111010101001").toString()); Assert.assertEquals("10 1010 0101 1110 1010 1001", textHighlighter.process("1010100101111010101001").toString());
Assert.assertEquals("1010100101111010101001", textHighlighter.process("101 010 01 0 111 1 0 10101001").toString()); Assert.assertEquals("10 1010 0101 1110 1010 1001", textHighlighter.process("101 010 01 0 111 1 0 10101001").toString());
} finally { } finally {
me.setNumeralBase(NumeralBase.dec); me.setNumeralBase(NumeralBase.dec);
} }

View File

@ -388,7 +388,7 @@ public class CalculatorEngineTest {
} }
cm.getEngine().setNumeralBase(NumeralBase.hex); cm.getEngine().setNumeralBase(NumeralBase.hex);
Assert.assertEquals("637B", cm.evaluate(JsclOperation.numeric, "56CE+CAD").getResult()); Assert.assertEquals("63 7B", cm.evaluate(JsclOperation.numeric, "56CE+CAD").getResult());
Assert.assertEquals("E", cm.evaluate(JsclOperation.numeric, "E").getResult()); Assert.assertEquals("E", cm.evaluate(JsclOperation.numeric, "E").getResult());
} finally { } finally {
cm.setNumeralBase(defaultNumeralBase); cm.setNumeralBase(defaultNumeralBase);

View File

@ -66,7 +66,7 @@ public class FromJsclSimplifyTextProcessorTest {
Assert.assertEquals("20x", tp.process("20x")); Assert.assertEquals("20x", tp.process("20x"));
Assert.assertEquals("2×0x3", tp.process("2*0x3")); Assert.assertEquals("2×0x3", tp.process("2*0x3"));
Assert.assertEquals("2×0x:3", tp.process("2*0x:3")); Assert.assertEquals("2×0x:3", tp.process("2*0x:3"));
Assert.assertEquals("0x:3000000", tp.process("0x:3 000 000.00000000000001")); Assert.assertEquals("0x:3 00 00 00", tp.process("0x:3 000 000.00000000000001"));
} }
} }