Math operations with Embedded Data and Survey Input Data to output in currency form | XM Community
Skip to main content

Hello,
I am working on a survey where the outputs from math operations are rather large, and currently I have been using Qualtrics math operations to get an output. The output results are in the range of a few hundred thousand to a several million. I know through JavaScript it is possible to have survey output in a comma separated numeric form or currency form. The issue I am running into now is that my JavaScript does not show up when I am making the calculation, it just shows up as blank. my code currently looks like this:

var site_earthwork_small= parseInt("${q://QID43/ChoiceTextEntryValue/1}");
var site_utilities_small=parseInt("${q://QID58/ChoiceTextEntryValue/1}");
var site_paving_small=parseInt("${q://QID46/ChoiceTextEntryValue/1}");
var site_landscaping_small=parseInt(" ${q://QID47/ChoiceTextEntryValue/1}");
var small_building_1=parseInt(" ${e://Field/Small%20Building%20size}");
var site_small_cost1=parseInt((site_earthwork_small+site_utilities_small+site_paving_small+site_landscaping_small)*small_building_1);
Qualtrics.SurveyEngine.setEmbeddedData("site_small_cost1", site_small_cost1);

You shouldn't parse the equation - it is already a number. Also, you had spaces at the front of a couple of fields. Try:
var site_earthwork_small= parseInt("${q://QID43/ChoiceTextEntryValue/1}");
var site_utilities_small=parseInt("${q://QID58/ChoiceTextEntryValue/1}");
var site_paving_small=parseInt("${q://QID46/ChoiceTextEntryValue/1}");
var site_landscaping_small=parseInt("${q://QID47/ChoiceTextEntryValue/1}");
var small_building_1=parseInt("${e://Field/Small%20Building%20size}");
var site_small_cost1=(site_earthwork_small+site_utilities_small+site_paving_small+site_landscaping_small)*small_building_1;
Qualtrics.SurveyEngine.setEmbeddedData("site_small_cost1", site_small_cost1);


Hi TomG,
Thank you for the quick response. I tried your code and I get a blank output in my survey field when I pipe in a response. Would that be a code issue or an embedded data issue?


https://community.qualtrics.com/XMcommunity/discussion/comment/52076#Comment_52076Are you trying to pipe it on the same page you are calculating it? If so, you can't do that - pipes are resolved on the server before the page is sent to the browser. You could use JS to update the contents of an html element instead of piping.
The other issue could be if one of your fields parses as NaN (not a number).


https://community.qualtrics.com/XMcommunity/discussion/comment/52079#Comment_52079I am trying to pipe into a page after the inputs. It shows as blank when the page loads, but then if I go to the next page and come back, then the math loads in as the piped text.


https://community.qualtrics.com/XMcommunity/discussion/comment/52081#Comment_52081That's because, as I said above, you are trying to pipe the value on the same page you are calculating it, and you can't do that.
Add html something like this to the question with the JS:

Then add this line of JS after your calculation:
jQuery("#calc").text(site_small_cost1);


https://community.qualtrics.com/XMcommunity/discussion/comment/52082#Comment_52082That worked, thank you so much! is there an easy way to get that output into either a currency format, or numeric with commas?


https://community.qualtrics.com/XMcommunity/discussion/comment/52084#Comment_52084You can use wNumb.js. You can load wNumb in the survey header from jsDelivr.


https://community.qualtrics.com/XMcommunity/discussion/comment/52086#Comment_52086Thank you, so Jsdelivr is added to the JavaScript portion of the survey header? From there when I try to use the wNumb formatting I get an error for an unexpected identifier. I suspect it is the "to" identifier. I attached my code for reference
var site_earthwork_small= parseInt("${q://QID43/ChoiceTextEntryValue/1}");
var site_utilities_small=parseInt("${q://QID58/ChoiceTextEntryValue/1}");
var site_paving_small=parseInt("${q://QID46/ChoiceTextEntryValue/1}");
var site_landscaping_small=parseInt("${q://QID47/ChoiceTextEntryValue/1}");
var small_building_1=parseInt("${e://Field/Small%20Building%20size}");
var site_small_cost_1string=(site_earthwork_small+site_utilities_small+site_paving_small+site_landscaping_small)*small_building_1;
var moneyformat1=wNumb({ mark: '.', thousand: ',' prefix: '$ ', suffix: 'USD'});
var site_small_cost1=moneyformat1.to( site_small_cost_1string )
Qualtrics.SurveyEngine.setEmbeddedData("site_small_cost1", site_small_cost1);
jQuery("#calc").text(site_small_cost1);


You are missing a comma after thousand:
var moneyformat1=wNumb({ mark: '.', thousand: ',', prefix: '$ ', suffix: 'USD'});


https://community.qualtrics.com/XMcommunity/discussion/comment/52096#Comment_52096That worked! Thank you so much for the help!


Leave a Reply