java code for showing decimal number form between text by 2 decimal places | XM Community
Skip to main content
Solved

java code for showing decimal number form between text by 2 decimal places

  • May 11, 2023
  • 5 replies
  • 89 views

Forum|alt.badge.img+2

I need a Java scripts which can show the generated decimal form number between text in preview just by two decimal part not more.

Best answer by Shashi

We can use Math operation as show below:

product_price - ${rand://float/1:5}

product_price_round - $e{ round( e://Field/product_price , 2 ) }

 

5 replies

Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • May 11, 2023

"You bought a product for 1.6358959564557056 since that was less than or equal to the price you offered to 4" this is the exact text qualities are going to show to participants when they answer the question and I am adding a java code in question part to make the 1.6358959564557056 to just 1.63


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 11, 2023

If your html is:

You bought a product for <span class="amt">1.6358959564557056</span> since that was less than or equal to the price you offered to 4

Then you can use this JS:

var amt = jQuery("#"+this.questionId+" .amt");
amt.text(Number(amt.text()).toFixed(2));

 


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • May 12, 2023

If your html is:

You bought a product for <span class="amt">1.6358959564557056</span> since that was less than or equal to the price you offered to 4

Then you can use this JS:

var amt = jQuery("#"+this.questionId+" .amt");
amt.text(Number(amt.text()).toFixed(2));

I tried the code but it is not working, let me explain more, “1.6358959564557056” is a number which is Generated randomly by computer as i sat is as embedded data in flow survey: product_price= ${rand://float/1:5}, and As i can not put anything else unless math logic in flow survey i need a java code to reduce the decimal place to 2 places for me because as you can see when participants see this message “You bought a product for 1.6358959564557056 since that was less than or equal to the price you offered to 4” in preview is annoying.

 


Shashi
Level 8 ●●●●●●●●
Forum|alt.badge.img+34
  • Level 8 ●●●●●●●●
  • Answer
  • May 12, 2023

We can use Math operation as show below:

product_price - ${rand://float/1:5}

product_price_round - $e{ round( e://Field/product_price , 2 ) }

 


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • May 12, 2023

If your html is:

You bought a product for <span class="amt">1.6358959564557056</span> since that was less than or equal to the price you offered to 4

Then you can use this JS:

var amt = jQuery("#"+this.questionId+" .amt");
amt.text(Number(amt.text()).toFixed(2));

I tried the code but it is not working, let me explain more, “1.6358959564557056” is a number which is Generated randomly by computer as i sat is as embedded data in flow survey: product_price= ${rand://float/1:5}, and As i can not put anything else unless math logic in flow survey i need a java code to reduce the decimal place to 2 places for me because as you can see when participants see this message “You bought a product for 1.6358959564557056 since that was less than or equal to the price you offered to 4” in preview is annoying.

 

Did you try it with this html?

You bought a product for <span class="amt">${e://Field/product_price}</span> since that was less than or equal to the price you offered to 4