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 Roundthe 79.90 to 79 ?

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

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

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

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?

Hi, how can I write a javascript code that creates random numbers from one to ten so that by clicking on a button, this JavaScript command does not repeat the code that it generates, for example, if the first clicks 3 were shown, the next clicks would not be 3 displayed. .Thanks

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.

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.

## 20 Replies

See this thread here for info.

thanks

Pierre-Andre

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

Will try, Thanks

Pierre-Andre

Hi,

Sorry but it does not work for me. I still get a decimal after the period.

Thanks anyway.

Pierre-Andre

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

Rounding.storyHi

Got it, it works

Thanks

Pierre-Andre

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

Hi

I tried it but it is not working for me. I'm not sure where go wrong. I'm using Storyline 3, are you using the same?

Thanks.

Can you post your .story file or at least the code you are using?

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!

I haven't tested, but something like this should work for rounding to the nearest 10.

var player = GetPlayer();

var targetNumber = player.GetVar("MyNumber");

targetNumber = targetNumber/10;

targetNumber = Math.round(targetNumber);

targetNumber = targetNumber*10;

player.SetVar("MyNumber", targetNumber);

Thanks Owen, I will try out.

Hi Owen

I just want to say thank you! The javascript works perfectly.

Hi, how can I write a javascript code that creates random numbers from one to ten so that by clicking on a button, this JavaScript command does not repeat the code that it generates, for example, if the first clicks 3 were shown, the next clicks would not be 3 displayed. .Thanks

round_picture.storyYou 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.

Thanks a lot Owen!

You are most welcome.

Thanks a lot owen

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.