use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.
the class FormulaParser method getNumberPtgFromString.
/**
* Get a PTG for an integer from its string representation.
* return Int or Number Ptg based on size of input
*/
private static Ptg getNumberPtgFromString(String number1, String number2, String exponent) {
StringBuilder number = new StringBuilder();
if (number2 == null) {
number.append(number1);
if (exponent != null) {
number.append('E');
number.append(exponent);
}
String numberStr = number.toString();
int intVal;
try {
intVal = Integer.parseInt(numberStr);
} catch (NumberFormatException e) {
return new NumberPtg(numberStr);
}
if (IntPtg.isInRange(intVal)) {
return new IntPtg(intVal);
}
return new NumberPtg(numberStr);
}
if (number1 != null) {
number.append(number1);
}
number.append('.');
number.append(number2);
if (exponent != null) {
number.append('E');
number.append(exponent);
}
return new NumberPtg(number.toString());
}
use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.
the class FormulaParser method parseUnary.
private ParseNode parseUnary(boolean isPlus) {
boolean numberFollows = IsDigit(look) || look == '.';
ParseNode factor = powerFactor();
if (numberFollows) {
// + or - directly next to a number is parsed with the number
Ptg token = factor.getToken();
if (token instanceof NumberPtg) {
if (isPlus) {
return factor;
}
token = new NumberPtg(-((NumberPtg) token).getValue());
return new ParseNode(token);
}
if (token instanceof IntPtg) {
if (isPlus) {
return factor;
}
int intVal = ((IntPtg) token).getValue();
// note - cannot use IntPtg for negatives
token = new NumberPtg(-intVal);
return new ParseNode(token);
}
}
return new ParseNode(isPlus ? UnaryPlusPtg.instance : UnaryMinusPtg.instance, factor);
}
use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.
the class TestFormulaParser method testParseNumber.
@Test
public void testParseNumber() {
IntPtg ip;
// bug 33160
ip = (IntPtg) parseSingleToken("40", IntPtg.class);
assertEquals(40, ip.getValue());
ip = (IntPtg) parseSingleToken("40000", IntPtg.class);
assertEquals(40000, ip.getValue());
// check the upper edge of the IntPtg range:
ip = (IntPtg) parseSingleToken("65535", IntPtg.class);
assertEquals(65535, ip.getValue());
NumberPtg np = (NumberPtg) parseSingleToken("65536", NumberPtg.class);
assertEquals(65536, np.getValue(), 0);
np = (NumberPtg) parseSingleToken("65534.6", NumberPtg.class);
assertEquals(65534.6, np.getValue(), 0);
}
use of org.apache.poi.ss.formula.ptg.NumberPtg in project poi by apache.
the class TestFormulaParser method confirmUnary.
private static void confirmUnary(String formulaText, double val, Class<?>... expectedTokenTypes) {
Ptg[] ptgs = parseFormula(formulaText);
confirmTokenClasses(ptgs, expectedTokenTypes);
Ptg ptg0 = ptgs[0];
if (ptg0 instanceof IntPtg) {
IntPtg intPtg = (IntPtg) ptg0;
assertEquals((int) val, intPtg.getValue());
} else if (ptg0 instanceof NumberPtg) {
NumberPtg numberPtg = (NumberPtg) ptg0;
assertEquals(val, numberPtg.getValue(), 0.0);
} else {
fail("bad ptg0 " + ptg0);
}
}
Aggregations