Reputation: 13
I'm working with a Text Item screen (type="5"
) that includes Answers for integer numeric input (attributeType="2"
). Now, when I perform a calculation with the user input values, the return value is always a float numeric.
Shortened example:
<question key="#1" type="5" title="My Screen">
<answer key="#1_1" attributeType="2" nextQuestionKey="#2">
<text>Num1</text>
</answer>
<answer key="#1_2" attributeType="2" nextQuestionKey="#2">
<text>Num2</text>
</answer>
<text>Enter 2 numeric values</text>
<onLeaveOkPersistAssignment>
val1 = getAnswerValue($answer:'#1_1'); <!-- for example '1' -->
val2 = getAnswerValue($answer:'#1_2'); <!-- for example '2' -->
$local:result = val1 + val2;
</onLeaveOkPersistAssignment>
</question>
<question key="#2" type="0" title="My Screen">
<answer key="#2_1" nextQuestionKey="END">
<text>%PLC%</text>
</answer>
<text>The result is:</text>
<onEnterAssignment>
setPlaceholder('%PLC%', $local:result); <!-- the screen should display '3' not '3.0' -->
</onEnterAssignment>
</question>
Is there a possibility to return an integer or at least convert the value into an integer?
Upvotes: 0
Views: 62
Reputation: 608
The easiest approach I can think of is the following:
$local:result = (val1 + val2) \ 1;
Simple integer division by 1 gets rid of the decimal rest and works on numerics and not only strings as parseInt does.
Upvotes: 0
Reputation: 81
what you can use is the method to convert the given format into int
myInt = parseInt(answerVal, 10)
in your case it will look like that:
<onLeaveOkPersistAssignment>
val1 = getAnswerValue($answer:'#1_1'); <!-- for example '1' -->
val2 = getAnswerValue($answer:'#1_2'); <!-- for example '2' -->
val1 = parseInt(val1, 10);
val2 = parseInt(val2, 10);
$local:result = val1 + val2;
</onLeaveOkPersistAssignment>
Upvotes: 0