Solved
JavaScript toFixed Question
I am trying to adapt some code from this thread so that I can pull the numeric value from a question and post it to an embedded data value with always having 2 decimal places (think US currency).
> ```
> Qualtrics.SurveyEngine.addOnload(function() {
> var score = parseFloat("${gr://SC_cvbYKS7ZMqwXmgR/Score}");
> score = Math.round(score * 10) / 10;
> Qualtrics.SurveyEngine.setEmbeddedData("score1", score.toFixed(1));
> });
> ```
I've tried the following codes, on UnLoad, and doesn't seem to work. Any suggestions?
> ```
> var Q8 = ("${q://QID8/ChoiceTextEntry/Value}");
> var Q8 = Q8.toFixed(2);
>
>Qualtrics.SurveyEngine.setEmbeddedData("score.Q8", Q8);
>```
and
> ```
> var Q8 = ("${q://QID8/ChoiceTextEntry/Value}");
>
>Qualtrics.SurveyEngine.setEmbeddedData("score.Q8", Q8.toFixed(2);
>```
Best answer by TomG
You need to convert the piped string to a number to use toFixed.
```
Qualtrics.SurveyEngine.addOnload(function() {
var Q8 = parseFloat("${q://QID8/ChoiceTextEntry/Value}");
Qualtrics.SurveyEngine.setEmbeddedData("score.Q8", Q8.toFixed(2));
});
```
I'm confused about the Unload comment. I'm assuming it is a typo.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
