Skip to main content
Participant
September 30, 2016
Question

Formatting variable numbers to display with commas

  • September 30, 2016
  • 1 reply
  • 158 views

I have created some variables with large numbers and making calculations with them. These variable are displaying in my course but are unformatted with commas. I know this can be down with math functions in javascript but need help.

function numberWithCommas(x) {
  
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

    This topic has been closed for replies.

    1 reply

    TLCMediaDesign
    Inspiring
    September 30, 2016

    You are going to need to put the function I gave you in the HTML page in the head section just above the:

    function initializeCP()

    In the JS window you can add the variables together as an argument for the function:

    var result = numberWithCommas(window.variable_1+window.variable_2);

    window.yourFinalVariable = result;