Rounding numbers in Storyline

Sep 27, 2017

Hello,

A variable is getting a result of (let's say) 79.90 after adding a few other variables together.... but I do not want what's after the decimal. Does anyone how to Round the 79.90 to 79 ?

Thanks

Pierre-Andre

20 Replies
OWEN HOLT

Are you open to using JavaScript? Do you always want it rounded DOWN? 

If so: 
var player = GetPlayer(); //locates the SL player and stores player object in a variable
var targetNumber = player.GetVar("your storyline variable's name"); // get your number from SL
targetNumber = Math.floor(targetNumber); //rounds down to whole number
player.SetVar("your storyline variable's name", targetNumber); //sends the rounded number back to your SL variable.

If you want it rounded up or down (basic math rules of rounding):
var player = GetPlayer(); //locates the SL player and stores player object in a variable
var targetNumber = player.GetVar("your storyline variable's name"); // get your number from SL
var baseNumber = Math.floor(targetNumber); //rounds down to whole number
var upOrdown = targetNumber - baseNumber //used to determine if next whole number should be used
if (upOrdown >= .5) {
targetNumber = baseNumber +1
}; //adds 1 to the number you rounded down instead of up if the decimal is greater than or equal to 5
player.SetVar("your storyline variable's name", targetNumber); //sends the rounded number back to your SL variable

OWEN HOLT

Sorry about that, left off a critical else statement.
var player = GetPlayer();
var targetNumber = player.GetVar("MyNumber");
var baseNumber = Math.floor(targetNumber);
var upOrdown = targetNumber - baseNumber
if (upOrdown >= .5) {
    targetNumber = baseNumber +1
    } else{
    targetNumber = baseNumber
    };
player.SetVar("MyNumber", targetNumber);

 

OWEN HOLT

Simplified code using the Math.round function. Replace MyNumber in the code below with the name of your SL variable. This will round up or down to the nearest integer.

var player = GetPlayer();
var targetNumber = player.GetVar("MyNumber");
targetNumber = Math.round(targetNumber);
player.SetVar("MyNumber", targetNumber);

Megan Yeo

Hi Owen

I have re-tried again and turned out that it works. I missed out certain parameters so apologies for my error.

However, I would like my number to round off slightly differently though. For example, the number is 88.88%. Using the above javascript you have provided, the number rounded off to 89%. But I would like the number to round off to 90%. If the number is 66.66%, I would like the number to round off to 70%. Can you please guide me as how to do so?

Thanks again for your help!

OWEN HOLT

You will need a variable for each possible number that you are going to choose from with a default value of 1. When they are selected randomly, you will need to change the value of the appropriate variable to 0. You will use these variables to create an array and then filter the array to remove the 0 values. This will then let you select a random number from the filtered (unselected) array. 
You can read more about it and see it in action in this post: Trivia Game - Using Variables and JavaScript to remove answered questions from the deck.

OWEN HOLT

No problem.  You might check out the simplified code/process I came up with for generating a non-repeating random number from a range of numbers here: Link

It uses fewer StoryLine variables and lets JavaScript do more of the "heavy lifting".   This allows you to use a much larger range of numbers without requiring a large number of variables. In fact, it will only ever require 3 of them, regardless of how large your range is.

This discussion is closed. You can start a new discussion or contact Articulate Support.