rounding errors in custom code values | XM Community
Solved

rounding errors in custom code values

  • 23 February 2021
  • 3 replies
  • 6 views

Userlevel 1

Screen Shot 2021-02-23 at 3.44.00 PM.pngI am working with some custom code where I have participants enter a symmetric confidence interval (in Q112) around a value they input in a previous question (in Q111, specifically ${q://QID111/ChoiceNumericEntryValue/1}). I want all of these values that show up in Q112 to have 1 decimal place. Right now, Q111 allows for 1 decimal place, and this is reflected in "QR~QID112~2," but the confidence interval appears rounded to the nearest whole number ("QR~QID112~3" and "QR~QID112~4"). How can I edit the code below such that I get values with the correct decimal place (1 decimal) rather than rounded values ?

JS for Q112:
Qualtrics.SurveyEngine.addOnload(function()
{
document.getElementById("QR~QID112~2").disabled = true;
document.getElementById("QR~QID112~3").disabled = true;
document.getElementById("QR~QID112~4").disabled = true;
  var val="${q://QID111/ChoiceNumericEntryValue/1}";
document.getElementById("QR~QID112~2").value = val;

setInterval(function(){ 

var val_1=parseInt(document.getElementById("QR~QID112~1").value);
var val_2=parseInt(document.getElementById("QR~QID112~2").value);

var val_3=val_2-val_1;
var val_4=val_1+val_2;

document.getElementById("QR~QID112~3").value=val_3;
document.getElementById("QR~QID112~4").value=val_4;

}, 100);

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});





JS for Q113 (in case relevant):
Qualtrics.SurveyEngine.addOnload(function()
{

setInterval(function(){ 

var val_main="${q://QID111/ChoiceNumericEntryValue/1}";

    jQuery("#QID113").hide();


var val_1=parseInt(document.getElementById("QR~QID112~1").value);

if(val_main<26 && val_1<=val_main)

      document.getElementById("QR~QID113").value=1;
}

else if(val_main>25 && val_1<=50-val_main)
{
document.getElementById("QR~QID113").value=1;
}

else
{
document.getElementById("QR~QID113").value=0;
}


}, 100);

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

icon

Best answer by ahmedA 23 February 2021, 22:06

View original

3 replies

Userlevel 7
Badge +21

You are using

parseInt()
, which is short for parse integer. So, your decimals are getting dropped. Here's how to get one decimal place.
Replace
parseInt(something here)
with
Number(Number(something here).toFixed(1))

Userlevel 1

https://www.qualtrics.com/community/discussion/comment/34895#Comment_34895ahmedA very helpful!!! I am now occasionally getting too many decimals now? Is there a way to make sure it's exactly 1 decimal?
Screen Shot 2021-02-23 at 5.24.59 PM.pngHere's the code i'm using:

Qualtrics.SurveyEngine.addOnload(function()
{
document.getElementById("QR~QID112~2").disabled = true;
document.getElementById("QR~QID112~3").disabled = true;
document.getElementById("QR~QID112~4").disabled = true;
  var val="${q://QID111/ChoiceNumericEntryValue/1}";
document.getElementById("QR~QID112~2").value = val;

setInterval(function(){ 

var val_1=Number(Number(document.getElementById("QR~QID112~1").value).toFixed(1));
var val_2=Number(Number(document.getElementById("QR~QID112~2").value).toFixed(1));

var val_3=val_2-val_1;
var val_4=val_1+val_2;

document.getElementById("QR~QID112~3").value=val_3;
document.getElementById("QR~QID112~4").value=val_4;

}, 100);

});

Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/

});

Userlevel 7
Badge +21

I'm pretty sure if you look at the script, you'll figure out why that is happening. Once you do that, you can use the code above to handle it.

Leave a Reply