Adding commas to long numbers in Storyline

I played around with adding commas to long numbers in Storyline to make numbers a bit more readable. Here is the solution:

Here is the JavaScript I used on a JavaScript trigger. I have a variable in Storyline called SLNumber (the number someone types in), one called SLAnswer (the final answer you will get in Storyline), and a JavaScript variable called JSAnswer.

var player = GetPlayer();
var JSAnswer = player.GetVar("SLNumber");

JSAnswer = numberWithCommas(JSAnswer);

player.SetVar("SLAnswer",JSAnswer);

function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

 

6 Replies
XAn Choly
David Charney

I played around with adding commas to long numbers in Storyline to make numbers a bit more readable. Here is the solution:

Here is the JavaScript I used on a JavaScript trigger. I have a variable in Storyline called SLNumber (the number someone types in), one called SLAnswer (the final answer you will get in Storyline), and a JavaScript variable called JSAnswer.

var player = GetPlayer();
var JSAnswer = player.GetVar("SLNumber");

JSAnswer = numberWithCommas(JSAnswer);

player.SetVar("SLAnswer",JSAnswer);

function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

 

Hey David, great work, thanks! Is there a way to replace the contents of the numeric text field with the comma'd values when the field loses focus?