How to append additional zeros after the comma in a variable using javascript? | XM Community
Question

How to append additional zeros after the comma in a variable using javascript?

  • 25 January 2021
  • 8 replies
  • 43 views

Badge +1

Hello,
I would like to append additional zeros after the comma in a variable, in order to display it to participants. Since I have to many subcases to display, doing it by hand using the survey flow doesn´t seem feasible. I have tried to write a javascript code in which I recall my variable from the embedded data field, apply for example the "toFixed(x)" function, and save it back to embedded data. From what I understand, the problem of this approach is that, when qualtrics saves the variable from the javascript back to the embedded data, it considers it as a numerical variable and hence it´s unable to take into account the zeros that I´ve added (since mathematically they don´t make sense). Is there a way to add additional zeros after the comma of a variable and then display it? Or alternatively, is there a way to save a variable from javascript to qualtrics without using embedded data?
Thanks.


8 replies

Userlevel 7
Badge +27

If you just need to format it to display it to participants, you don't need to save it as an embedded data field. You can just format it on the fly using something like wNumb.js.
Pipe them (you can do one or more on a page) into a tag(s) in your survey question:
${e://Field/myvar}
Format it (where dec3formt is the wNumb format you define):
jQuery(".decimals3").each(function() {
var numEl = jQuery(this);
numEl.text(dec3formt.to(Number(numEl.text())));
});

Badge +1

Hi TomG ,
Thanks.
From what I understand, before I have to import in qualtrics the wNumb.js. library, right?
How do I do it?

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/33677#Comment_33677Add wNumb.js to your survey header. Go to jsdelivr.com find wNumb and jsdelivr will give you a


It doesn´t seem to work as qualtrics displays it as a normal text at the beginning of each block of the survey. Consequenty, also the following piece of code that I wrote in the javascript section (in the block where the variable with 2 decimals should be displayed) doesn´t seem to work:
Qualtrics.SurveyEngine.addOnload(function()
{
jQuery(".decimals3").each(function() {
   var numEl = jQuery(this);
   numEl.text(dec3formt.to(Number(numEl.text())));
});   
});
What am I doing wrong?
Thank you very much!

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/33682#Comment_33682You weren't in source mode when you edited the header. You need to click the <> icon.

Badge +1

Ok, now the header seems to be loaded correctly!
the following piece of code seems to be still read as normal text:
${e://Field/payoff}
I wrote this in the block where I want my variable to be displayed. In the same block (in the javascript section) I wrote the other piece of code surrounding it with the "addOnLoad" function.

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/33684#Comment_33684If the span is in the question text, you need to edit in "HTML View".

Badge +1

Now it works. Thank you very much!

Leave a Reply