Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Formatting variable numbers to display with commas

New Here ,
Sep 30, 2016 Sep 30, 2016

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, ",");
}

137
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Sep 30, 2016 Sep 30, 2016
LATEST

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources