Format Integers – Insert Commas

[Total: 0    Average: 0/5]

This will format form fields by inserted commas in them.

Head Code:

<script>
function convert(x)
{
  var _x=x.toString();
  var _len=_x.length;
  var _ret="";
  for(var i=_len;i>0;i--)
  {
    if(!((_len-i)%3))
    {
      _ret=","+_ret;
    }
    _ret=_x[i-1]+_ret;
  }
  _ret=_ret.substr(0,_ret.length-1);
  return _ret;
}
function doit()
{
 var _f=document.f1;
 var _n1=parseInt(_f.n1.value);
 var _n2=parseInt(_f.n2.value);
 var _answer=_n1*_n2;
 _f.a.value=convert(_answer);

 _f.b1.value=convert(_f.b1.value);
 _f.b2.value=convert(_f.b2.value);
 _f.b3.value=convert(_f.b3.value);
 _f.b4.value=convert(_f.b4.value);
 _f.b5.value=convert(_f.b5.value);
 _f.b6.value=convert(_f.b6.value);
 _f.b7.value=convert(_f.b7.value);
 _f.b8.value=convert(_f.b8.value);
 _f.b9.value=convert(_f.b9.value);
 _f.b10.value=convert(_f.b10.value);
}
</script>

Body Code:

Click the equal button to see the numbers transformed.
<form name=f1>
<input name=n1 value=1290>
*
<input name=n2 value=1780>
<input type=button value="=" onclick="doit()">
<input name=a value="?">
<HR>
<input name=b1 value="0">
<input name=b2 value="90">
<input name=b3 value="890">
<input name=b4 value="7890">

<input name=b5 value="67890">
<input name=b6 value="567890">
<input name=b7 value="4567890">
<input name=b8 value="34567890">
<input name=b9 value="234567890">
<input name=b10 value="1234567890">
</form>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *