Cannot get my math operation to round | XM Community
Skip to main content
I have a math formula within an ED field that I am trying to round to 3 decimal places. Here is how I have it currently setup:



$e{ round( q://QID106%231/SelectedAnswerRecode/1 * 2.24146232) / ( q://QID10/ChoiceTextEntryValue * .49 ) - (.017 * q://QID106%232/SelectedAnswerRecode/1 , 3 ) }



The formula is calculating correctly and I am not receiving any error message, but it also is not rounding at all. Does anyone know how to fix this?



Thanks!
Try this:



$e{ round(( q://QID106%231/SelectedAnswerRecode/1 * 2.24146232) / ( q://QID10/ChoiceTextEntryValue * .49 ) - (.017 * q://QID106%232/SelectedAnswerRecode/1 , 3 ) }



If that doesn't work, I would guess it's getting confused with the nesting and would recommend you save that formula as its own embedded data field and then use round on that ED.
It didn't work. I'll try creating separate variables. Thanks.
Try this - I think your order of operations were off.



$e{ round( (q://QID106%231/SelectedAnswerRecode/1 * 2.24146232) / (( q://QID10/ChoiceTextEntryValue * .49 ) - (.017 * q://QID106%232/SelectedAnswerRecode/1 , 3 ))) }



I think your last statement |"- (.017 * q://QID106%232/SelectedAnswerRecode/1 , 3 )"| was being applied after the rounding.
@Akdashboard, it still didn't work. That's okay though. Thank you for looking into it.
Try this:

```

$e{ round ( ( q://QID106%231/SelectedAnswerRecode/1 * 2.24146232 ) / ( q://QID10/ChoiceTextEntryValue * 0.49 ) - ( 0.017 * q://QID106%232/SelectedAnswerRecode/1 ) , 3 ) }

```
@tomG got it to work!
woot woot! Go @TomG! TomG is the man!

(I've had coffee this morning).
Hey guys,



Does anyone knows how I can force the round to show the number of decimals?

For example, if i have 4,97 and I want to round to 1 decimal place, it shows me the number 5, but I want it to show me the number 5,0.



Can anyone help me?



Thank you very much!
> @Vitorrm said:

> Hey guys,

>

> Does anyone knows how I can force the round to show the number of decimals?

> For example, if i have 4,97 and I want to round to 1 decimal place, it shows me the number 5, but I want it to show me the number 5,0.

>

> Can anyone help me?

>

> Thank you very much!



You'll have to do it in JS. Use toFixed(1).
Hi Tom,



I am trying to use Java, but i don't know exactly how to do it, and my tests are not working.



What i have figured out so far is that i have to use an earlier question to calculate the variables I want to show at the end.



I want to round and force 1 decimal on 5 Scores or a variables, witch examples of codes are: ${gr://SC_cvbYKS7ZMqwXmgR/Score} and ${e://Field/Score%20geral}.



But so far, I can not even create a simple variable using Java.

I tried this just to see if i could create a variable, but i still can't find the variable "teste" in my pipe text (embedded data):



Qualtrics.SurveyEngine.addOnPageSubmit(function()

{

var inputs = $(this.questionId).select('.InputText');

var name1 = inputs[0].value;

{

Qualtrics.SurveyEngine.setEmbeddedData("Name", name1);

}

});




Can you see what i am doing wrong and help with the code to use toFixed?



Thank you very much for your help!
If you want the embedded variable saved in your response data you have to first define it as an embedded data in your survey flow. After you do that, you can change the value using JavaScript.



In your example above the name of the embedded variable is "Name" not "teste".



So, let's say you want round a score to 1 decimal place:

1. Define the embedded data variable "score1" in the survey flow

2. Use JS to update score1 as a score rounded to 1 decimal place:

```

Qualtrics.SurveyEngine.addOnload(function() {

var score = parseFloat("${gr://SC_cvbYKS7ZMqwXmgR/Score}");

score = Math.round(score * 10) / 10;

Qualtrics.SurveyEngine.setEmbeddedData("score1", score.toFixed(1));

});

```
@TomG, Thank you very much!



It worked perfectly!



Thanks

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.

image.pngimage.pngimage.pngin action:
Question 1
image.pngQuestion 2, next block
image.pngData:
image.png
Thank you!!


A couple of things:

  1. Number doesn't give you anything...it is the same as the answer to Q1.

  2. 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!)
toFixed.JPG


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"?


  1. https://community.qualtrics.com/XMcommunity/discussion/comment/40785#Comment_40785Is your JS inside the addOnload or addOnReady function?

  2. Are there any errors in the console? (Right click/Developer Tools/Inspect/Console)

  3. After the var CheckTotal line add
    console.log(CheckTotal);
    Do you get the expected value?


  1. https://community.qualtrics.com/XMcommunity/discussion/comment/40790#Comment_40790It's in the "addOnUnload" function only right now.

  2. "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

  3. 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 🙄


  1. 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.

  2. That error isn't from your script. It is from a Qualtrics script...don't worry about it.

  3. 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


Leave a Reply