我正在使用包含整数数字输入答案(type="5"
)的文本项屏幕(attributeType="2"
)。现在,当我使用用户输入值执行计算时,返回值始终是浮点数。
缩短的例子:
<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>
是否有可能返回一个整数或至少将该值转换为整数?
答案 0 :(得分:0)
您可以使用的是将给定格式转换为int
的方法myInt = parseInt(answerVal, 10)
在你的情况下它看起来像这样:
<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>
答案 1 :(得分:0)
我能想到的最简单的方法如下:
$local:result = (val1 + val2) \ 1;
简单整数除以1除去了十进制数,并且对数字有效,而不仅仅是像parseInt那样的字符串。