Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q8 Is it possible to output a number e.g. 1234567 as 1,234,567?

You are here: irt.org | FAQ | JavaScript | Number | Q8 [ previous next ]

<script language="JavaScript"><!--
function outputComma(number) {
    number = '' + number
    if (number.length > 3) {
        var mod = number.length%3;
        var output = (mod > 0 ? (number.substring(0,mod)) : '');
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0))
                output+= number.substring(mod+3*i,mod+3*i+3);
            else
                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
        }
        return (output);
    }
    else return number;
}

document.write(outputComma(1)+'<br>');
document.write(outputComma(12)+'<br>');
document.write(outputComma(123)+'<br>');
document.write(outputComma(1234)+'<br>');
document.write(outputComma(12345)+'<br>');
document.write(outputComma(123456)+'<br>');
document.write(outputComma(1234567)+'<br>');
document.write(outputComma(12345678)+'<br>');
document.write(outputComma(123456789)+'<br>');
document.write(outputComma(1234567890)+'<br>');
//--></script>

Another solution is given below:

<script language="JavaScript"><!--
function reverseIt(str) {
   if (!str) return; // nothing to change
   var rstr = '';
   for (i=str.length-1;i>=0;i--) rstr += str.charAt(i);
   return rstr;
}
function thousands(str) {
   var saveStr = "" + str;
   if (saveStr.length < 4) return str;
   var revStr = reverseIt(saveStr);
   var newStr = '';
   for (var i=0;i<revStr.length;i++) {
      if (i>0 && (i%3)==0) newStr += ',';
      newStr += revStr.charAt(i);
   }
   return reverseIt(newStr);
}
//--></script>

<form>
<input type="text" name="inText">
<input type="text" name="outText">
<input type="button" onClick="this.form.outText.value=thousands(this.form.inText.value);" value="thousands">
</form>

Val Todorov writes:

A more elegant solution:

<script language="JavaScript1.1"><!--
function number_format(n) {
  var arr=new Array('0'), i=0;
  while (n>0)
    {arr[i]=''+n%1000; n=Math.floor(n/1000); i++;}
  arr=arr.reverse();
  for (var i in arr) if (i>0) //padding zeros
    while (arr[i].length<3) arr[i]='0'+arr[i];
  return arr.join();
}
//--></script>
<form>
<input type="text" name="input">
<input type="text" name="output">
<input type="button"
onClick="this.form.output.value=number_format(this.form.input.value)" VALUE="Number Format">
</form>

The following was Submitted by Will Schleter

Some of the other routines don't work if the numbers are negative or include decimals. Here is a fairly simple routine that seems to work.

function outputComma(number) {
  str = number.toString();
  i = str.indexOf(".");
  if (i<1) {
    i=str.length;
  }
  while (i>3) {
    i-=3;
    j = str.charAt(i-1);
    if (j>="0" && j<="9") {
      str = str.substr(0,i) + "," + str.substr(i);
    }
  }

  return str;
}

Feedback on 'Q8 Is it possible to output a number e.g. 1234567 as 1,234,567?'

©2018 Martin Webb