How to force the decimal places for values created by embeded value. | XM Community
Skip to main content

Dear all,

I am trying to design a choice list whose right-hand-side is an increasing numerical values. The numerical values are created by mathmatically operating embeded values like below:

 £$e{e://Field/EV_unit_01 * X} 

X is from a sequence from 1 to 19, while e://Field/EV_unit_01 is 0.05 in my case. As you can imagine, the resulting numerical vector is 0.05, 0.1, 0.15, …. However, I am trying to make it look like 0.05, 0.10, 0.15, …, with fixing 2 decimal places, so that the whole list will like better. 

I have tried with the rounding, which does not work for my case. Some answer in earlier posts suggest javascript, but I still have no clue how to do it.

Could you please share your knowledge here? Thank you for your attention.

Regards,

Elon

@Jilong_Wu Try this code

Qualtrics.SurveyEngine.addOnload(function() {
var sourceValue = "${e://Field/EV}";
var formattedValue = parseFloat(sourceValue).toFixed(2);
Qualtrics.SurveyEngine.setEmbeddedData("FormattedEV", formattedValue);
});

The embedded data EV and FormattedEV is my example, change it to yours
Let me know if it helps


 Hi @dxconnamnguyen,

Thanks for your answer. I have adapted and use your code as below.

Qualtrics.SurveyEngine.addOnload(function() {
    var sourceValue = "${e://Field/EV_unit_01}";
    var formattedValue = parseFloat(sourceValue).toFixed(2);
    Qualtrics.SurveyEngine.setEmbeddedData("Formatted_EV", formattedValue);
});

However, it does not work.

If I understand correctly, the term "Formatted_EV" is another embeded value. I firstly set Embedded Data Formatted_EV as “value will be set from Panel or URL” in the survey flow, and then I write the text as

<span style="font-size:18px;"><span style="font-family:Arial,Helvetica,sans-serif;">obtain<strong> &pound;$e{e://Field/Formatted_EV * 1}</strong> for sure</span></span> 

However, this does not give what I expect, but shows “obtain £0 for sure” on page. Then, after some googlework, I notice that someone says we cannot “set the value of an Embedded Data and call it using piped text within the same question” (see this). Thus, I simply add a pure text question with a page break. Wow, I can see the original number again, but the digital number is not fixed to 2 (i.e., still 0.05, 0.1, ...). So, it seems that toFixed function does not work.

Could you please have a second look over this? Thank you.


If you want to use the value inserted on the same page use page submit for validation in JS section of your question.


@Jilong_Wu

Not actually, my code just format what you put in, so if you put 0.1 in sourceValue it will return 0.10 in formattedValue. If you continue to make a math calculate with it ($e{e://Field/Formatted_EV * 1}) , it will go back to the default format. So just pipe it as ${e://Field/Formatted_EV} and you will see that it worked.


And yes, you should pip-text it to the next question after set it up with JavaScript. Anyway, since I don’t know about your case, how you will use it. That’s my example case with embedded data, the essential of it is the JavaScript function parseFloat(sourceValue).toFixed(2);  

You can adapt it to your case by send it in html element that you’re trying to show in each of your choices


@dxconnamnguyen 

Thanks for the reply.

I see your point here. However, this is not my intention. My choice lists have 19 rows. That’s why I need to introduce the embedded values such as 0.01 and 0.05, so that I can get various corresponding choice lists (such as from 0.01 to 0.19, and, from 0.05 to 0.95) easily without setting it manually. So, if I need to format the value for each rows of each list, it will be a heavy burden. 

I am wondering whether it is possible to apply the toFixed function to the html element of the choice lists? My choice lists are “Side by side” type question, and thus the subject I want to edit is span.LabelWrapper (please see below figure). Do you have any idea how to achieve this?

Demo of my choice list

Thank you

 


Hi @Jilong_Wu ,

How and where is your multiplier “X” defined?


Hi, @vgayraud 

The “X” (i.e., 1, 2, …, 19) is entered into the option label as below:

Thanks


I think you should store everything in EDs:

  • EV_unit : set from 0.01 to 0.05
  • EV_unit_01 : $e{ e://Field/EV_unit * 1 }
  • EV_unit_02 : $e{ e://Field/EV_unit * 2 }
  • EV_unit_19 : $e{ e://Field/EV_unit * 19 }

And then use @dxconnamnguyen ‘s script to create 19 formatted EDs that you’ll pipe in in your question.


@vgayraud 

Thanks for replying.

So, seemingly there is no way to set that globally. I wish Qualtrics can make the round function more advanced in the future.


@Jilong_Wu,

You need to do the formatting on the page where you are presenting the data.

First, put all the numbers to format in spans with the same class:

<span class="fixed2">$e{ e://Field/EV_unit_01 * 1 }</span>

Then format them:

Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+this.questionId+" .fixed2").each(function() {
var el = jQuery(this);
el.text(parseFloat(el.text()).toFixed(2));
});
});

 


Hi, @TomG 

 

Thanks for your attention. I have tried to paste your codes to the option label and JS place respectively. Specifically, I tried the JS codes as below:

Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+this.QID25+" .fixed2").each(function() {
var el = jQuery(this);
el.text(parseFloat(el.text()).toFixed(2));
});
});

where QID25 is the question ID. It turns out that the zero are still eliminated (i.e., still 0.1 not 0.10).

Besides, I also tried 

Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+QID25+" .fixed2").each(function() {
var el = jQuery(this);
el.text(parseFloat(el.text()).toFixed(2));
});
});

and directly pasted the codes you provided without any other style setups to exclude any potential influences.

<span class="fixed2">$e{ e://Field/EV_unit_01 * 1 }</span>

However, I still cannot see any change.

Could you please instruct me where I am wrong?

 

Best,


Hi, @TomG 

 

Thanks for your attention. I have tried to paste your codes to the option label and JS place respectively. Specifically, I tried the JS codes as below:

Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+this.QID25+" .fixed2").each(function() {
var el = jQuery(this);
el.text(parseFloat(el.text()).toFixed(2));
});
});

where QID25 is the question ID. It turns out that the zero are still eliminated (i.e., still 0.1 not 0.10).

Besides, I also tried 

Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#"+QID25+" .fixed2").each(function() {
var el = jQuery(this);
el.text(parseFloat(el.text()).toFixed(2));
});
});

and directly pasted the codes you provided without any other style setups to exclude any potential influences.

<span class="fixed2">$e{ e://Field/EV_unit_01 * 1 }</span>

However, I still cannot see any change.

Could you please instruct me where I am wrong?

 

Best,

Your mistake was trying to replace this.questionId with QID25 (which would have caused an error since this.QID25 or QID25 is an undefined variable). Leave the code exactly as I posted it, this.questionId is a variable with the value “QID25”.


Inspried by @vgayraud and @dxconnamnguyen, there is another way to solve this problem partially, and I want to share it in case someone happens to need it.

Qualtrics.SurveyEngine.addOnload(function() {

const len = 19;
const sourceValue = "${e://Field/wholerange_unit}";

for(let i=0;i<len;i++){
var formattedValue = parseFloat(sourceValue * (i+1)).toFixed(2);
Qualtrics.SurveyEngine.setEmbeddedData("Formatted" + i, formattedValue);
}

});

With above codes, a series of embedded value “Formatted0”, …, “Formatted18” will be created (Please remember to set them in survey flow too). They then can be used.


Hi @rgupta15,

 

Could you please share how do you set the validation in JS section?

Thank you.

 

Best, 


@TomG 

Hi, I have also tried to paste your codes without any change, which still shows 0.1 not 0.10. This is also the reason why I was trying to replace it with QID25.

Regards,

 


@TomG

Hi, I have also tried to paste your codes without any change, which still shows 0.1 not 0.10. This is also the reason why I was trying to replace it with QID25.

Regards,

 

The script works.  You have an error somewhere. Maybe you attached the script to the wrong question? 

Demo (the script runs on the second page):

https://marketinview.yul1.qualtrics.com/jfe/preview/previewId/92e92078-1b9d-4c2d-8b23-2d3ee15f332d/SV_0MuxIIulufYOzZj/BL_57uiv3Glj1sDDcG?Q_SurveyVersionID=current


@Jilong_Wu Tom’s idea is to use .toFixed to format what inside html class="fixed2" so you have to edit the html tag where you show that number (Rich Content Editor-->Code). It’s more concise and exactly what I suggest you to adapt in to your case.
Anyway, glad you found the way with embedded data.


 

Hi @TomG,

Thanks for your patient about my post.

I try to locate the mistake, and I realize it may be caused by the question type:

As you can see, I have add the fixed2 tag to all three places

screen shot for question editing

However, I can only see the tag style works for the question lable but not option label.

Preview page

Could you please have a third look on it? 

Thank you


@Jilong_Wu,

It is very strange. For some reason, the side-by-side question is undoing the update. Luckily, the fix is easy - just change addOnload to addOnReady.


Leave a Reply