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!
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("."); }
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!
Thanks, Luciana! I was able to get in touch with Steve Gannon who provided assistance for the post you referenced and he quickly helped me out. :) This community is the best!!
11 Replies
try
var number = 12344556567.1264567;
var ret = number.toLocaleString('en-US', { maximumFractionDigits: 2 })
console.log( ret );
result: "12,344,556,567.13"
Hi Jurgen,
I tried your recommendation but that did not work for me. :-/
Thanks for the suggestion though.
What does your JS look like, and what is actually happening when you try it?
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(".");
}
Attached is a screenshot of how it appears at one point. I would like it to either show $49,237.50 or round to $49,238.
Your screenshot did not get attached. you usually have to use a browser to attach a file.
Better share a sample... easiest to help.
Trying to attach the screenshot again here.
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!
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!
Thanks, Luciana! I was able to get in touch with Steve Gannon who provided assistance for the post you referenced and he quickly helped me out. :) This community is the best!!