How to Get Two Decimal Places or Cut Decimals with JavaScript in Storyline?

Sep 28, 2022

Hi, 

I am trying to figure out how to get my number variable to either have two decimal places, or simply have the decimals cut and just round to the nearest whole number. I was able to achieve getting it to have commas through JavaScript. Attached is what I have so far. Any help will be greatly appreciated!

 

Thanks,

 

Holly

11 Replies
Holly Castellow

I have JavaScript working to add a comma to the number variable. This is what that current JS looks like: 

 

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

JSAnswer = numberWithCommas(JSAnswer);

player.SetVar("TotalSavedTxt",JSAnswer);

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

Holly Castellow

Hello! Getting back around to trying to figure this out. Attached is part of a prototype I had developed last year as a Storyline sample. If selecting a savings of 25%, the total saved at milestone 1 shows as $82,062.5. I am trying to get JavaScript to either make that display as $82,062.50 or round it to $82,063. Help with this will be much appreciated! 

Luciana Piazza

Hi Holly! 

Thanks so much for sharing your file with us!

I wanted to pop in a share a related community discussion that provides a JavaScript workaround that could be of help: 

I'll leave it to our knowledgeable community for any JavaScript tips and tricks!