• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Problem to set variable decimal characters to 3

Explorer ,
Dec 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

Hello everyone!

I have a little problem with displaying the content of a variable.

I have a self-made survey, which ends with a slide that must show 4 results. Each of them, are stored in 4 variables. This results are numbers that are in between 0 and 1.

When a number like 0.666666666667 appear, I need it to be shown as 0.66 or 0.67 (it doesn't matter if it's just cropped). When I setted the variable, I put 3 as the maximum number of characters, but when I previewed it, it shows more numbers (you can see it in the capture).

Is there anyway to set it up to show just 2 decimals, and not every of them?

Thank you in advance!

I'm using Captivate 2019, with the last update.

captura1.jpg

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Dec 19, 2018 Dec 19, 2018

Oops, my bad. One little thing needs to be added to make the round function work:

varcat1_2decimals = Math.round(var_categoria1 * 100)/100;

Votes

Translate

Translate
Community Expert ,
Dec 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

Yes this can be done if your content output is HTML5 and you are willing to invest in the CpExtra HTML5 widget.

Take a look at this page:

Math Related | Infosemantics Pty Ltd

The function you need is related to xcmndRoundTo

If you don't want the widget, you'll need to work out how to do the math using JavaScript.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

Hi Rod! Thank you for the quick response!

Unfortunatelly I cannot invest in the widget.

I'll see if I can find someone to help me with JS, 'cause I don't think I'll be able to do it (my knowledge about JS is about 0 ).

Thank you so much again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Several functions/methods are available in JS to define the number of decimals. Do you want that number to be variable or fixed?

BTW, I never have seen a widget that was so powerful as CpExtra. Its cost is ridiculously low compared with the many hours of time you would save by avoiding a lot of scripting. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Hi Lilybiri, I have to make an stupid question, what's the difference between the number being variable or fixed?

I have a variable wich value can be 0.5 (one decimal) or 0.389474636463824. I the case the value is 0.5 I need just to show 0.5, in the case the number is 0.389474636463824, I need to show 0.38 (cropped is just fine, if it round it to 0.39 it's fine too).

Thank you in advance.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Was talking about the number of deciimals. There is a method .toFixed() but you can make the number of deciamls variable. If you alway s want 3 deciamls, it is a fixed number.

My bad explanation.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

I always want the same number of decimals, so fixed!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

As usual there are many ways to achieve the same result. If you don't need the result for further calculations in Captivate, the methiod I mentioned (.roFixed) wouid have been easier. I would have chosen that one, but both methods work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

I was trying the method Dan suggested before, but cannot make it work.

For sure I'm doing something wrong. Paste here the JS: (window, actual)

var_categoria1 = window.cpAPIInterface.getVariableValue(var_categoria1);

varcat1_2decimals = round(var_categoria1 * 100)/100;

window.cpAPIInterface.setVariableValue(var_categoria1, varcat1_2decimals);

var_categoria2 = window.cpAPIInterface.getVariableValue(var_categoria2);

varcat2_2decimals = round(var_categoria2 * 100)/100;

window.cpAPIInterface.setVariableValue(var_categoria2, varcat2_2decimals);

var_categoria3 = window.cpAPIInterface.getVariableValue(var_categoria3);

varcat3_2decimals = round(var_categoria3 * 100)/100;

window.cpAPIInterface.setVariableValue(var_categoria3, varcat3_2decimals);

var_categoria4 = window.cpAPIInterface.getVariableValue(var_categoria4);

varcat4_2decimals = round(var_categoria4 * 100)/100;

window.cpAPIInterface.setVariableValue(var_categoria4, varcat4_2decimals);

In fact, if I want to use the .toFixed method you mentioned, how apply it? Something like this? (1 variable for example)

var_categoria4 = window.cpAPIInterface.getVariableValue(var_categoria4);

var_categoria4 = var_categoria4.toFixed(2);

window.cpAPIInterface.setVariableValue(var_categoria4);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Wanted to leave this to the user who posted his solution. But I see that you are not using the setVariableValue/getVariableValue correctly. The argument indicating the variable you want to get or to populate has to be between quotes (he used double quotes).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Yes, you must use quotes when using a Captivate name in JS. Single or double quotes do the same thing - it doesn't matter.

And yes, you used toFixed() properly. The old-school way is what I grew up with, so I still use it - it also makes clear what is happening mathematically.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Thank you so much for your help Lily & Dan!

Even with the quotes, doesn't seem to work.

var_categoria1 = window.cpAPIInterface.getVariableValue("var_categoria1");

varcat1_2decimals = round(var_categoria1 * 100)/100;

window.cpAPIInterface.setVariableValue("var_categoria1", varcat1_2decimals);

var_categoria2 = window.cpAPIInterface.getVariableValue("var_categoria2");

varcat2_2decimals = round(var_categoria2 * 100)/100;

window.cpAPIInterface.setVariableValue("var_categoria2", varcat2_2decimals);

var_categoria3 = window.cpAPIInterface.getVariableValue("var_categoria3");

varcat3_2decimals = round(var_categoria3 * 100)/100;

window.cpAPIInterface.setVariableValue("var_categoria3", varcat3_2decimals);

var_categoria4 = window.cpAPIInterface.getVariableValue("var_categoria4");

varcat4_2decimals = round(var_categoria4 * 100)/100;

window.cpAPIInterface.setVariableValue("var_categoria4", varcat4_2decimals);

In the Advanced Action, when execute the JS, I selected "actual window". Is that correct?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2018 Dec 18, 2018

Copy link to clipboard

Copied

Maybe you can make use of the knowledge in this video. Adobe Captivate - Progress Indicator as a Percentage - YouTube

Paul Wilson, CTDP

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

I assume those values are stored in variables - I'll call them var1, var2, var3 ....

On Enter of the slide, execute Javascript

//First get your value from Captivate to JS:

var1 = window.cpAPIInterface.getVariableValue("var1");

//To round to 2 decimal places you multiply each value by 100, round it to a whole number, then divide by 100:

var1Rounded  = round(var1 * 100);

var1_2decimals = var1Rounded/100;

//Or combine that in one line:

//var1_2decimals = round(var1 * 100)/100;

//Send that back to Captivate:

window.cpAPIInterface.setVariableValue("var1", var1_2decimals);

//Repeat for the other variables

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Oops, my bad. One little thing needs to be added to make the round function work:

varcat1_2decimals = Math.round(var_categoria1 * 100)/100;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 19, 2018 Dec 19, 2018

Copy link to clipboard

Copied

Great!

Worked like a charm!

Thank you, thank you, thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 13, 2019 Nov 13, 2019

Copy link to clipboard

Copied

Can you give me the three lines that I need to put in, that edit threw me off, I am not sure where that goes

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 13, 2019 Nov 13, 2019

Copy link to clipboard

Copied

Can you give just the javascript command exactly how I need to input? The numbers are just not rounding

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2020 Sep 07, 2020

Copy link to clipboard

Copied

Hi

Did you manage to solve the javascript command issue? I am having the same issue in wanting to round a Captivate variable to two decimal places - getting nowhere fast!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2020 Sep 07, 2020

Copy link to clipboard

Copied

Managed to get the Captivate variable 2 decimal places result I needed by using the following
(with a kind nod to Paul Wilson and his contact!):

 

window.cpAPIInterface.setVariableValue
("myVarNam",Math.floor(myVarNam * 100) / 100);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2020 Sep 07, 2020

Copy link to clipboard

Copied

LATEST

You didn't want to round, just to cut? The method "number.tofixed(2)" would have meant lot less typing than this math function. Any method is good if it works however

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources