Forum Discussion
A Solution to rounding in Storyline – without using Javascript
Normal 0 false false false oNotPromoteQF /> EN-US X-NONE X-NONE
I found a way to round numbers in Storyline without using Javascript. The challenge was to show whole percentage numbers from calculated decimal numbers (i.e. 33% for 1/3 and 67% for 2/3). Searching through the forums, all I could find was Javascript solutions and suggestions to submit a feature request. Not happy with either of these answers, I continued to play around.
I found that Storyline only holds 7 digits past the decimal place, and the 7th digit is rounded. So to get a rounded decimal (percentage) as a whole number, divide by 100,000, and then multiply by 10,000,000.
For example:
2 / 3 = 0.6666667 (rounded at the 7th position by Storyline)
0.6666667 / 100,000 = .000006666667 (which is rounded to 0.0000067)
0.0000067 * 10,000,000 = 67
Now we have our rounded 67% from 2/3
You should be able to apply this to any number you want to round. Divide and then multiply by a multiple of 10 that is large enough to push the number you want to round to the 7th position past the decimal (i.e. 13,567 / 10,000,000,000 * 10,000,000,000 = 14,000)
- ChrisElkinCommunity Member
Can anyone help me find a fix to my problem.
I have been happily using the rounding up solution posted above (great fix BTW) but I've run into an issue.
I have a screen that calculates stuff. The main calculation is taking a number and dividing it by 30%. This solution has worked a treat until we entered the value 30.
30% of 30 = 9 but this solution because of the divide / multiply scenario returns the incorrect value of 10.
So whilst rounding up is fab in most situations I need a solution that allow me to also return the correct value?
NE1?
- DianaMyersCommunity Member
@Chris Elkin - can you share any part of your file/screens/triggers? And can you clarify if you are multiplying by 30% or dividing by 30%?
30 multiplied by .30 = 9
30 divided by .30 = 100 (if you're using this formula and rounding to two digiits, it would explain why the result returned was 10)
Hope to hear from you soon.
- ChrisElkinCommunity Member
- PhilMayorSuper Hero
It will always show 10 because of the way Storyline calculates to 7 decimal places, you are turning the number into 8 decimal places which rounds up to 0.0000001, this may be where you need to use javascript instead.
- ChrisElkinCommunity Member
Cheers Phil, IU thought this might the case!!!!
So has anyone got an example of a JS rounding script?
- MichaelHinzeCommunity Member
See attahced a file that uses JS to round the input from a numeric entry.
- SandyDutraCommunity Member
I tried applying this JavaScript and it doesn't seem to work. I have an answer that shows as (i.e.) 10.454240362 and I want it to round up to 10.45
How can I apply a variable/JavaScript to make this work?
Thanks
Sandy
- ChrisElkinCommunity Member
Hi - thanks for this script.
I've added the additional line to the code:
var numTimes = numValue * 0.3;
and now I don't get a rounded whole number with every calculation
e.g.
enter 100 returns 30 - cool
enter 30 returns 9 - cool
enter 54 returns 16.2 - ??? would like it to round unto 17
NE1?
_________________________________________________________________________
var numValue = player.GetVar("howMany");
var numTimes = numValue * 0.3;
var JSRoundedNum = numTimes.toFixed(1);
player.SetVar("total",JSRoundedNum);
- KyleTrailCommunity Member
I ended up just creating the variables and dividing then multiplying them. It worked like a charm.
- AllisonAntalek-Community Member
All you seriously have to do is delete the first set - %Results.ScorePercent%% - and then remove the parentheses and then all you get is the total amount of points. Hope this helps! Allison
- StephaniePownerCommunity Member
Please does anyone know if the number of decimal places stored has changed? I have published a course in Storyline 1 before and using the dividing/multiply method and all worked fine. Have now published from Storyline 2 and I get the two decimal places again. Also, I am now using HTML5 output. Thanks
- YoncaUral1Community Member
Hi,
As someone who doesn't really know much about Javascript, I'm trying to figure out how to solve a problem I'm having. In our course, we have a variable that we are displaying on each page to show the % complete. We did the divide by 10,000,000 then multiply by 10,000,000 and it's working fine non-mac devices. However Macs are still displaying the two decimal places and we want to get rid of that. So, I tried to add this JS after my % visited variable is calculated and it's not working.
Here is what I added:
var player = GetPlayer();
var numValue = GblScreensVisitedPercentage
var JSRoundedNum = numValue.toFixed(1);
player.SetVar("RoundedNum",JSRoundedNum);Any thoughts would be greatly appreciated!
- JodieJones1Community Member
Wow. I was skeptical, but this worked great! Thank you soooooooo much.