TomG I've been community-stalking you a bit on this topic. I've been following this and another thread and cannot seem to get mine to work.
I have a text field that I need to perform some math on, then show/record as ED with two decimal points (even if they are .00)
In my survey flow are ED fields called "Number" and "Decimal" (which I have formatted as numnbers). First question is text entry, asking for a number (dollar amount, if you will). "Number" then gets assigned the response to question 1. In question 2, which I have on a second block, I want that response/"Number" to show with two decimal points. I also want the ED field "Decimal" to be populated with that two-decimal number.
I'm using the following JS, but while Question 2 shows 0.00 (so not the value I'm looking for, but two decimal points), the "Decimal" ED records as simply "0". The example below shows the JS in an OnPageSubmit, but I've also tried it in the OnLoad of both Question 1 and Question 2 - even tried it on a placeholder question between them to no avail.
Clearly I'm doing something wrong, missing a step, or have angered the JS Gods.

in action:
Question 1
Question 2, next block
Data: 
Thank you!!
A couple of things:
- Number doesn't give you anything...it is the same as the answer to Q1.
- You don't need a math operation ($e) to get the value of Q1. You can just pipe it. It would have worked except you had the pipe wrong (QID1, not QID3) and you needed a space after { and before }.
Try:
Qualtrics.SurveyEngine.addOnload(function() {
var Bonus = parseFloat("${q://QID1/ChoiceTextEntry}");
Qualtrics.SurveyEngine.setEmbeddedData("Decimal",Bonus.toFixed(2));
});
TomG Thanks so much for following up, you definitely got me further than I was getting (it's always something dumb like the wrong QID!). Had to work on some other things for a bit, but I've finally gotten the value to round to two decimals, however, it doesn't force two decimals. So I still get values like 25 or 54.1 - I need those to force two decimals. Am I doing something wrong, or is that not how toFixed(2) is meant to work?
Here is the code I'm using (thank you again for your help there!)
Did you define BonusTB and InitialTB as numbers in the survey flow? If so, change them to text.
Naturally!
"...but, to have decimals surely it needs to be a Number... I'm so prepared!" Waahhh Wahhhhhh...
Thanks, TomG! Total Wizard!!
TomG I know this is an old post, but I'm trying to figure this out. This JavaScript is not working for me... I'm completely new to JavaScript so I had someone else who knows how to use JS look at it and he can't figure out why it's not working.
var CheckTotal = parseFloat("$e{ round ( ( e://Field/ExtraBooks + 40 ) , 2 ) }");
Qualtrics.SurveyEngine.setEmbeddedData("DollarAmount",CheckTotal.toFixed(2));
Any ideas?
Sidenote... Qualtrics should totally make "dollar amount" a "feature" or "math operation" for embedded data variables.
https://community.qualtrics.com/XMcommunity/discussion/comment/24177#Comment_24177TomG any pointers on how to "define" embedded data variables as text/string instead of numbers in survey flow?
DebbyLewis Not Tom here, but I can help you with that. In your Embedded data box click on options in the lower right corner. Then you can choose the variable type for each ED field.
https://community.qualtrics.com/XMcommunity/discussion/comment/40787#Comment_40787perfect! It looks like they are all set to "Text" by default. So I don't think that was my issue. Thanks for teaching me something new about survey flow uhrxx005 Any insights into why my JS "won't run"?
- https://community.qualtrics.com/XMcommunity/discussion/comment/40785#Comment_40785Is your JS inside the addOnload or addOnReady function?
- Are there any errors in the console? (Right click/Developer Tools/Inspect/Console)
- After the var CheckTotal line add
console.log(CheckTotal);
Do you get the expected value?
- https://community.qualtrics.com/XMcommunity/discussion/comment/40790#Comment_40790It's in the "addOnUnload" function only right now.
- "Failed to evaluate page script in page Uncaught TypeError: Cannot read properties of null (reading 'outerHeight') https://osu.az1.qualtrics.com/jfe/static/dist/jfe.f4a27c51de2e3bd51abb.js:1:107795
- did not get the expected value after adding the suggested line.
Thanks for the assist. Really wish I didn't have to ask for help for something that should be really simple 🙄
- https://community.qualtrics.com/XMcommunity/discussion/comment/40792#Comment_40792Since your JS doesn't rely on any data from the current page, move it to addOnload.
- That error isn't from your script. It is from a Qualtrics script...don't worry about it.
- After moving to addOnload, if you still aren't getting the expected value, the problem is with your piped string.
TomG THANK YOU SO MUCH FOR THE ASSIST! Played around with it a little bit more. It made a difference which "question" my JS was added to. I'm not exactly sure why it's working now, but it *is* working. I did end up moving it to "addOnload" function and testing it a few times. Sometimes it showed correctly and sometimes it didn't. I moved the embedded data variables around in SurveyFlow until they started to show up correctly, consistently. Thanks again! Debby
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
