Forum Discussion

HappilyCreative's avatar
HappilyCreative
Community Member
3 years ago

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

  • 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.

  • 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! 

    • HappilyCreative's avatar
      HappilyCreative
      Community 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!!