function NumberFormat(){
this.decimals = 2;
this.preMask = "";
this.postMask = "";
this.commas = false;
this.toString = nf_tostring;
this._digitize = nf_digitize;
this._insertCommas = nf_insCommas;
this._parseDigits = nf_parseDigits;
function nf_parseDigits( instr){
var sret = new String("");
for(nChr=0; nChr< instr.length; nChr++)
if ( !isNaN(instr.charAt(nChr)) )
sret += instr.charAt(nChr);
return sret;
}
function nf_tostring( instr){
if( 'string' != typeof(instr)) instr = new String(instr);
var nums = instr.split("."); //separate the number from the fraction
if ( nums.length > 2)
nums = new Array(nums[0], nums[1]);
// make sure we have only digits on both sides of the decimal
for(N=0; N< nums.length; N++)
nums[N] = this._parseDigits(nums[N]);
if ( this.commas)
nums[0] = this._insertCommas(nums[0]);
if ( 0 < this.decimals){
if ( 2 > nums.length)
nums[1] = new String("");
nums[1] = this._digitize( nums[1]);
}
else
nums = new Array(nums[0]);
var sRet = new String(this.preMask);
if ( nums.length > 1)
sRet += nums.join(".");
sRet += this.postMask;
return sRet;
}
function nf_digitize( digits){
var sret = new String("");
for(nChr=0; nChr< digits.length && sret.length < this.decimals; nChr++)
if ( !isNaN(digits.charAt(nChr)) )
sret += digits.charAt(nChr);
while( sret.length < this.decimals)
sret += "0";
return sret;
}
function nf_insCommas( lnum){
var rnum = new String(lnum);
var sVal = new String("");
// reverse the left number keeping only numeric values
for(nChr=rnum.length; nChr>-1; nChr--){
if ( !isNaN(rnum.charAt(nChr)) )
sVal += rnum.charAt(nChr);
}
// if there were no digits assign a zero
if ( 0 == sVal.length) sVal += "0";
// now reverse it again adding in commas
rnum = new String("");
for(nChr=0; nChr< sVal.length; nChr++){
rnum = sVal.charAt(nChr) + rnum;
switch( nChr){
case 2:
case 5:
case 8:
case 11:
if ( nChr < sVal.length -1)
rnum = "," + rnum;
break;
default:
break;
} // switch( for each comma)
} // for()
return rnum;
} // fn_insCommas()
} // NumberFormat class