计算金额的Js代码介绍

计算金额的Js代码介绍

<script language="JavaScript">

/////////////////////////////////////////
//“,”:如果有的话,看逗号到小数点(如果有的话)前有几位,则按几位划分整数部分
//“0”:如果该位上没有数字,就补0
//“#”:如果该位上有数字就输出数字,没有则不输出
/////////////////////////////////////////
function formatNumber(number,pattern)
{
var str= number.toString();
var strInt;
var strFloat;
var formatInt;
var formatFloat;
if(//./g.test(pattern))
{
formatInt= pattern.split('.')[0];
formatFloat= pattern.split('.')[1];
}
else
{
formatInt= pattern;
formatFloat= null;
}

if(//./g.test(str))
{
if(formatFloat!=null)
{
var tempFloat= Math.round(parseFloat('0.'+str.split('.')[1])*Math.pow(10,formatFloat.length))/Math.pow(10,formatFloat.length);
strInt= (Math.floor(number)+Math.floor(tempFloat)).toString();
strFloat= //./g.test(tempFloat.toString())?tempFloat.toString().split('.')[1]:'0';
}
else
{
strInt= Math.round(number).toString();
strFloat= '0';
}
}
else
{
strInt= str;
strFloat= '0';
}
if(formatInt!=null)
{
var outputInt= '';
var zero= formatInt.match(/0*$/)[0].length;
var comma= null;
if(/,/g.test(formatInt))
{
comma= formatInt.match(/,[^,]*/)[0].length-1;
}
var newReg= new RegExp('(//d{'+comma+'})','g');

if(strInt.length<zero)
{
outputInt= new Array(zero+1).join('0')+strInt;
outputInt= outputInt.substr(outputInt.length-zero,zero)
}
else
{
outputInt= strInt;
}

var
outputInt= outputInt.substr(0,outputInt.length%comma)+outputInt.substring(outputInt.length%comma).replace(newReg,(comma!=null?',':'')+'$1')
outputInt= outputInt.replace(/^,/,'');

strInt= outputInt;
}

if(formatFloat!=null)
{
var outputFloat= '';
var zero= formatFloat.match(/^0*/)[0].length;

if(strFloat.length<zero)
{
outputFloat= strFloat+new Array(zero+1).join('0');
//outputFloat= outputFloat.substring(0,formatFloat.length);
var outputFloat1= outputFloat.substring(0,zero);
var outputFloat2= outputFloat.substring(zero,formatFloat.length);
outputFloat= outputFloat1+outputFloat2.replace(/0*$/,'');
}
else
{
outputFloat= strFloat.substring(0,formatFloat.length);
}

strFloat= outputFloat;
}
else
{
if(pattern!='' || (pattern=='' && strFloat=='0'))
{
strFloat= '';
}
}

return strInt+(strFloat==''?'':'.'+strFloat);
}

/////////////////////////////////////////
/////////////////////////////////////////
function FormatValue(thisform,obj,pattern)
{
var obj = eval('document.'+thisform+'.'+obj);
if (obj.value!=null)
{
obj.value = formatNumber(obj.value,pattern);
}
}

function count1()
{
FormatValue('thisform','dj','#.00');
var sl = document.thisform.sl.value;
var dj = document.thisform.dj.value;
if ((sl!="")&&(dj!=""))
{
document.thisform.je.value = sl*dj;
}
else
{
document.thisform.je.value = "0.00";
}
FormatValue('thisform','je','#.00');
}
</script>
<table width=100% cellspacing="1" cellpadding="5" align=center class="bg_tablemain"><form action="" method=POST name="thisform"><tr><td align=right>数量:</td><td><input type=text name=sl size=4 value="" onKeyUp="value=value.replace(//D+/g,'');count1();">(件)</td><td align=right>单价:</td><td><input type=text name=dj size=8 onKeyUp="count1();" value="0.00" >(元/件)</td><td align=right>金额:</td><td><input type=text name=je size=12 readonly value="" style="color: #CC0000">(元)自动统计</td></tr></form></table>