Forum Discussion
How to Get Two Decimal Places or Cut Decimals with JavaScript in Storyline?
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
- Jürgen_Schoene_Community Member
try
var number = 12344556567.1264567;
var ret = number.toLocaleString('en-US', { maximumFractionDigits: 2 })console.log( ret );
result: "12,344,556,567.13"
- HappilyCreativeCommunity Member
Hi Jurgen,
I tried your recommendation but that did not work for me. :-/
Thanks for the suggestion though.
- WaltHamiltonSuper Hero
What does your JS look like, and what is actually happening when you try it?
- HappilyCreativeCommunity Member
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(".");
} - HappilyCreativeCommunity Member
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.
- MathNotermans-9Community Member
Better share a sample... easiest to help.
- WaltHamiltonSuper Hero
Your screenshot did not get attached. you usually have to use a browser to attach a file.
- HappilyCreativeCommunity Member
- HappilyCreativeCommunity Member
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!
- HappilyCreativeCommunity Member
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!!