Forum Discussion

ThomasUmphres's avatar
ThomasUmphres
Community Member
10 months ago

Making Variables Round to Whole Number Rather Than to 2 Decimals

I have a pretty complex training I'm putting together using a lot of variables where numbers are being displayed with 1 or 2 decimal places. I want to force those numbers to show as rounded whole numbers. 

I saw in some old forum posts about passing said variable into a new variable, dividing by 10,000,000, then multiplying by 10,000,000, then passing it back into the original variable. But that did not work. The decimals remained.

Any way to easily do this? I have attached an example of the variables that I want to display as whole.

  • For best reliability, you need to pass them to Javascript, round them, and pass them back to a text variable in SL.

  • Walt,

    Thank you for your reply. Greatly appreciated.

    I have yet to use javascripts in Storyline. Any good resources you can point to as to how this script would be written? Trying ChatGPT but no luck so far. 

  • As of yet, it seems that you should stay away from ChatGPT, as you will probably have to train it. Here is a script that will work

    var player = GetPlayer();
     
    var numValue = player.GetVar("NumericEntry");
    var JSRoundedNum = numValue.toFixed(2);
    player.SetVar("RoundedNum",JSRoundedNum);
     
    NumericEntry is the name of your variable in SL, and RoundedNum is the name of the Text variable that it will return. (JS doesn’t distinguish between text and numeric variables, so SL considers them as text.)
  • Walt,

    Thanks so much. I got this working with a Javascript. I wish it was available in preview mode. I am just having to publish the individual slide to preview it. No biggie. 

    Also, this has opened my eyes to how powerful javascripts can be. They might be a huge time saver and do things I didn't think I could do going forward. 

    Thanks a ton.

  • You're welcome. PIAWYC  (Pass it along when you can). 
     
    Just my personal opinion - JS can be really powerful, but I only use it as an extreme last resort. When that project has to be maintained or modified, you never know if there will be somebody around with those programming skills.