Retaining zeroes after decimal point | XM Community
Solved

Retaining zeroes after decimal point

  • 23 December 2020
  • 8 replies
  • 62 views

Userlevel 1
Badge +6
  • Level 2 ●●
  • 16 replies

I'm attempting to calculate, record, and display the total incentive that survey respondents will receive depending on their performance on a game embedded in the survey. The process of calculating, recording, and displaying the respondent's incentive will repeat daily over 14 days.
If a participant records a score equal to or above 100 points on the game (as recorded in the ED day1_total), they will receive a $0.10 increment to their total incentive payment. For example, see the screenshot.
image.pngWhen using piped text ($${e://Field/incentive_total}) to display incentive_total to participants, the piped text does not retain zeroes that are unnecessary. That is, piping in incentive_total displays $0.1 or $ instead of $0.10 or $0.00.
Using the round operator does not seem to work.
There are a few approaches that have solved this using JS, but with minimal understanding of JS, I cannot seem to extrapolate the solutions from others' problems to my own.
Any help would be appreciated!

icon

Best answer by ahmedA 24 December 2020, 00:49

View original

8 replies

Userlevel 7
Badge +21

Once you have done all you editing. 
Go to the rich content editor and select the embedded data and change its colour. (Its temporary, so it can be anything)
Then come back to your question, select HTML view and delete the style tags and add an id, something like payment. See this:
image.png
Then add this JS to your question. All it does is, it takes your number, and ensures that it has two digits after decimals. It will add digits if there are less than two and rounds up if there are more than two. 
Qualtrics.SurveyEngine.addOnReady(function()
{
num = document.querySelector("#payment");
num.innerText = parseFloat(num.innerText).toFixed(2);
});
Make sure that the

id
you assign matches. In the example, I've used
payment
, but you can use anything. But whatever you use in the question text needs to be preceded by a
#
in the JS.

Userlevel 1
Badge +6

Excellent! Thank you very much.

Userlevel 1
Badge +6

ahmedA I'm returning to this project now and am running into a similar issue.
The solution you outlined does not seem to be working in one specific case.
I am using a math operation to create an embedded data variable "total_incentive_self", which calculates the current total incentive amount that a survey respondent has received. "total_incentive_self" is calculated by adding together all previous incentive amounts (which are either equal to "0.00" or "0.10"). See screenshot below for the math operation.
image.pngWhen presenting the value of "total_incentive_self" to survey respondents, the JS code you provided above does not seem to work as intended. The JS I am using for this question reads:
Qualtrics.SurveyEngine.addOnReady(function()
{
num = document.querySelector("#total_incentive_self");
num.innerText = parseFloat(num.innerText).toFixed(2);
});
If the respondent has earned $0.00 so far, the value displays as $0. Similarly, if the respondent has earned $0.10 so far, the value displays as $0.
Do you have any suggestions?
Thank you!

Userlevel 7
Badge +21

Try this:
num = document.querySelector("#total_incentive_self");
num.innerText = parseFloat("${e://Field/total_incentive_self}").toFixed(2);

Userlevel 1
Badge +6

ahmedA Dang, didn't seem to work. Same result.

Userlevel 7
Badge +21

Sorry, just relooked at the code. It isn't supposed to work. I've updated it. Please try that. If it also doesn't work. Can you share a screenshot of your question HTML?

Userlevel 1
Badge +6

ahmedA still no luck. Here's the HTML.
image.png

Userlevel 7
Badge +21

Put the value inside a span tag with the appropriate id, as explained in the photos, and it should work.

Leave a Reply