Cannot get my math operation to round

uhrxx005uhrxx005 Community Member, XMPN Member Guru ✭✭

Cannot get my math operation to round

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!

Best Answers

  • JenCXJenCX Broomfield, CO Wizard ✭✭✭✭✭
    edited July 2019 Accepted Answer

    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.

  • TomGTomG Raleigh, NC Wizard ✭✭✭✭✭
    edited July 2019 Accepted Answer

    Try this:

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

Answers

  • uhrxx005uhrxx005 Community Member, XMPN Member Guru ✭✭

    It didn't work. I'll try creating separate variables. Thanks.

  • AkdashboardAkdashboard South CarolinaCommunity Member Superuser ✭✭✭✭

    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.

  • uhrxx005uhrxx005 Community Member, XMPN Member Guru ✭✭

    @Akdashboard, it still didn't work. That's okay though. Thank you for looking into it.

  • uhrxx005uhrxx005 Community Member, XMPN Member Guru ✭✭

    @tomG got it to work!

  • AkdashboardAkdashboard South CarolinaCommunity Member Superuser ✭✭✭✭

    woot woot! Go @TomG! TomG is the man!
    (I've had coffee this morning).

  • VitorrmVitorrm São PauloCommunity Member Qubie ✭

    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!

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    @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).

  • VitorrmVitorrm São PauloCommunity Member Qubie ✭

    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!

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    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));
    });
    
  • VitorrmVitorrm São PauloCommunity Member Qubie ✭

    @TomG, Thank you very much!

    It worked perfectly!

    Thanks

  • KWiggKWigg SLC, UTCommunity Member Qubie ✭

    @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!!

  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    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));
    });
    
  • KWiggKWigg SLC, UTCommunity Member Qubie ✭

    @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!)


  • TomGTomG Raleigh, NCCommunity Member Wizard ✭✭✭✭✭

    Did you define BonusTB and InitialTB as numbers in the survey flow? If so, change them to text.

  • KWiggKWigg SLC, UTCommunity Member Qubie ✭

    Naturally!

    "...but, to have decimals surely it needs to be a Number... I'm so prepared!" Waahhh Wahhhhhh...

    Thanks, @TomG! Total Wizard!!

Sign In to Comment