Highlighted

## Problem to set variable decimal characters to 3

Explorer ,
Dec 18, 2018

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?

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

| Engaged

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

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

Views

350

Likes

Report

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

## Problem to set variable decimal characters to 3

Explorer ,
Dec 18, 2018

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?

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

| Engaged

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

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

Views

351

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 18, 2018 0
20 Replies 20
Dec 18, 2018

Copied

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

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.

Likes

Report

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

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!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 18, 2018 0
Most Valuable Participant ,
Dec 19, 2018

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.

Likes

Report

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

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 19, 2018 0
Most Valuable Participant ,
Dec 19, 2018

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.

Likes

Report

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

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 19, 2018 0
Most Valuable Participant ,
Dec 19, 2018

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.

Likes

Report

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

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);

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 19, 2018 0
Most Valuable Participant ,
Dec 19, 2018

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

Likes

Report

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

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.

Likes

Report

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

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?

Likes

Report

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

Copied

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

Paul Wilson, CTDP | https://captivateteacher.com

Likes

Report

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

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

Likes

Report

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

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;

Likes

Report

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

Copied

Great!

Worked like a charm!

Thank you, thank you, thank you!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 19, 2018 0
Explorer ,
Nov 13, 2019

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

Likes

Report

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

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 13, 2019 0
Community Beginner ,
Sep 07, 2020

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!

Likes

Report

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

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);

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 07, 2020 0
LATEST
Most Valuable Participant ,
Sep 07, 2020