Javascript number formatting help

Nov 07, 2018

Hi Javascripters

Please help, I have created a counter that goes up by 1803 every second using Javascript. This was no problem, but then I was asked if the number output could be formatted to have spaces in it so that it is easier to read.

I have tried a number of ways but cannot get it to do any kind of number formatting - just wondering if it is even possible with using a number variable in SL - should I use a text variable?

Here is what I have got to at the moment, counter goes up but does not format (I tried testing it in Codepen too and the formatting does not work):

var timer;
var counter = setInterval(timer, 1000);
var count = 0;
var final = count;

var player = GetPlayer();

function timer() {
count = count + 1803;
final = count;
result = final.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
player.SetVar("Countup_Display",final);
}

 

4 Replies
Daniel Servan

Do you want this formatting?
Please see the attached source Storyline file.

Also here's the Javascript.

var timer;
var counter = setInterval(timer, 1000);
var count = 0;
var final = count;
var myStr = "";

var player = GetPlayer();

function timer() {
count = count + 1803;
final = count;

myStr = final.toString();
player.SetVar("Countup_Display",final);
player.SetVar("FinalStr",myStr.charAt(0)+" "+myStr.charAt(1)+" "+myStr.charAt(2)+" "+myStr.charAt(3)+" "+myStr.charAt(4)+" "+myStr.charAt(5)+" "+myStr.charAt(6)+" "+myStr.charAt(7)+" "+myStr.charAt(8)+" "+myStr.charAt(9));
}

And Lastly, I'm not sure if this is the right thread to post here on Articulate Discussion. :)
But anyway, I'm here to help. Cheers!

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