*
* @return the value of the expression stored in a token
* @throws ParserException
- * @throws VariantException
*/
public Variant expr() throws ParserException {
final int entryStackLevel = this.operators.size();
} else {
value2 = term();
}
+ if (value2 == null) {
+ throw new ParserException(I18N.tr("unexpected end of string"), this.scanner.getLastToken(), false);
+ }
while (this.operators.size() > entryStackLevel
&& this.operators.lastElement().getOpCode().getPriority() >= op.getPriority()) {
reduceStack();
return value1;
}
+ /**
+ * Calculates the value of the expression.
+ *
+ * @param text
+ * formula text
+ * @return the value of the expression
+ * @throws ParserException
+ */
+ public Variant expr(String text) throws ParserException {
+ final Variant rc = reset(text).expr();
+ if (this.values.size() != 0) {
+ assert (this.values.size() == 0);
+ }
+ return rc;
+ }
+
/**
* Returns the variable given by the name.
*
*/
public Expression reset(String text) {
this.scanner.reset(text);
+ this.values.clear();
+ this.operators.clear();
return this;
}
if (precision <= 0) {
precision = 1;
}
- if (value >= 1E6) {
- rc = String.format("%.g", precision - 1, value);
- } else if (value > 1.0) {
- double limit;
- int prec = 0;
- switch (precision) {
- case 1:
- limit = 10.0;
- prec = 0;
- break;
- case 2:
- limit = 100.0;
- prec = 1;
- break;
- case 3:
- limit = 1000.0;
- prec = 2;
- break;
- default:
- limit = 1000.0;
- break;
- }
- if (value >= limit) {
- rc = String.format("%d", (int) Math.round(value));
- } else {
- rc = String.format("%.*g", prec, value);
- }
- } else if (value >= 1E-5) {
- final double limit;
- int prec;
- switch (precision) {
- case 1:
- limit = 1E-4;
- prec = 5;
- break;
- case 2:
- limit = 1E-3;
- prec = 4;
- break;
- case 3:
- limit = 1E-2;
- prec = 3;
- break;
- default:
- limit = 1E-2;
- prec = 3;
- break;
- }
- if (value < limit) {
-
- } else {
- rc = String.format("%.*f", prec, value);
- }
+ double upperLimit = 1E6;
+ double lowerLimit = 100;
+ double lowerLimit2 = 1e-4;
+ int prec = 0;
+ final int prec2 = 5;
+ switch (precision) {
+ case 1:
+ prec = 1;
+ upperLimit = 1E4;
+ lowerLimit = 10.0;
+ lowerLimit2 = 1e-2;
+ break;
+ case 2:
+ prec = 2;
+ upperLimit = 1E5;
+ lowerLimit = 100.0;
+ lowerLimit2 = 1e-3;
+ break;
+ case 3:
+ prec = 3;
+ lowerLimit = 1000.0;
+ break;
+ default:
+ upperLimit = 1000.0;
+ break;
+ }
+ if (value < 1.0) {
+ final String format = String.format("%%.%dg", prec);
+ rc = String.format(format, value).replace("e-0", "e-");
+ } else if (value < upperLimit && value >= lowerLimit) {
+ rc = String.format("%d", (int) Math.round(value));
} else {
- rc = String.format("%.*g", precision - 1, value);
+ final String format = String.format("%%.%dg", prec);
+ rc = String.format(format, value).replace("e+0", "e");
}
return rc;
}
private void checkPrec1() {
final double value = 1234567.77;
- Assert.assertEquals(StringUtils.formatFloat(value, 3), "123E6");
- Assert.assertEquals(StringUtils.formatFloat(123456.77, 3), "123456");
- Assert.assertEquals(StringUtils.formatFloat(12345.77, 3), "123456");
- Assert.assertEquals(StringUtils.formatFloat(1234.77, 3), "12345");
- Assert.assertEquals(StringUtils.formatFloat(123.77, 3), "123");
- Assert.assertEquals(StringUtils.formatFloat(12.77, 3), "12.7");
- Assert.assertEquals(StringUtils.formatFloat(1.77, 3), "1.77");
-
+ Assert.assertEquals(StringUtils.formatFloat(value, 1), "1e6");
+ Assert.assertEquals(StringUtils.formatFloat(123456.77, 1), "1e5");
+ Assert.assertEquals(StringUtils.formatFloat(12345.77, 1), "1e4");
+ Assert.assertEquals(StringUtils.formatFloat(1234.77, 1), "1235");
+ Assert.assertEquals(StringUtils.formatFloat(123.77, 1), "124");
+ Assert.assertEquals(StringUtils.formatFloat(12.77, 1), "13");
+ Assert.assertEquals(StringUtils.formatFloat(1.77222, 1), "2");
+ Assert.assertEquals(StringUtils.formatFloat(0.177222, 1), "0,2");
+ Assert.assertEquals(StringUtils.formatFloat(0.0177222, 1), "0,02");
+ Assert.assertEquals(StringUtils.formatFloat(0.00177222, 1), "0,002");
+ Assert.assertEquals(StringUtils.formatFloat(0.000177222, 1), "0,0002");
+ Assert.assertEquals(StringUtils.formatFloat(0.0000177222, 1), "2e-5");
}
private void checkPrec2() {
-
+ final double value = 1234567.77;
+ Assert.assertEquals(StringUtils.formatFloat(value, 2), "1,2e6");
+ Assert.assertEquals(StringUtils.formatFloat(123456.77, 2), "1,2e5");
+ Assert.assertEquals(StringUtils.formatFloat(12345.77, 2), "12346");
+ Assert.assertEquals(StringUtils.formatFloat(1234.77, 2), "1235");
+ Assert.assertEquals(StringUtils.formatFloat(123.77, 2), "124");
+ Assert.assertEquals(StringUtils.formatFloat(12.77, 2), "13");
+ Assert.assertEquals(StringUtils.formatFloat(1.77, 2), "1,8");
+ Assert.assertEquals(StringUtils.formatFloat(0.177222, 2), "0,18");
+ Assert.assertEquals(StringUtils.formatFloat(0.0177222, 2), "0,018");
+ Assert.assertEquals(StringUtils.formatFloat(0.00177222, 2), "0,0018");
+ Assert.assertEquals(StringUtils.formatFloat(0.000177222, 2), "0,00018");
+ Assert.assertEquals(StringUtils.formatFloat(0.0000177222, 2), "1,8e-5");
}
private void checkPrec3() {
-
+ final double value = 1234567.77;
+ Assert.assertEquals(StringUtils.formatFloat(value, 3), "1,23e6");
+ Assert.assertEquals(StringUtils.formatFloat(123456.77, 3), "123457");
+ Assert.assertEquals(StringUtils.formatFloat(12345.77, 3), "12346");
+ Assert.assertEquals(StringUtils.formatFloat(1234.77, 3), "1235");
+ Assert.assertEquals(StringUtils.formatFloat(123.77, 3), "124");
+ Assert.assertEquals(StringUtils.formatFloat(12.77, 3), "12,8");
+ Assert.assertEquals(StringUtils.formatFloat(1.77222, 3), "1,77");
+ Assert.assertEquals(StringUtils.formatFloat(0.177222, 3), "0,177");
+ Assert.assertEquals(StringUtils.formatFloat(0.0177222, 3), "0,0177");
+ Assert.assertEquals(StringUtils.formatFloat(0.00177222, 3), "0,00177");
+ Assert.assertEquals(StringUtils.formatFloat(0.000177222, 3), "0,000177");
+ Assert.assertEquals(StringUtils.formatFloat(0.0000177222, 3), "1,77e-5");
}
@Test