Search code examples
mobilemovilizer

Movilizer: How to get integer result after calculation with getAnswerValue() return values?


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?


Solution

  • 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.