ASP和Javascript中取整函数的应用
来源:asp之家 时间:2009-06-07 18:38:00
朋友的网站要计算机票的折扣价格,并且在最后的折扣价格上应对个位进行四舍五入,同时在ASP和Javascript代码中都需要实现这个功能,他们的操作并不一样:
ASP中实现四舍五入:
tmpAmount=tree_Cart.getItemInt(tCart.getItem("p_id"))
tmpPriceCurrent=int((cp.toProductPrice(tCart.getItemNum("p_price"),tCart.getItemInt("p_discount"))/10)+0.5)*10
tmpPrices=tmpPriceCurrent*tmpAmount
int(N)可以实现取整,而要四舍五入的话,int(N+0.5)可以实现。由于这里是要将个位数四舍五入,因此,先将原来的数值除以10,四舍五入后再乘以10就可以了。
Javascript中实现四舍五入:
由于Javascript中没有int函数,而是用Math.round(N)来实现:
appShop.toProductDiscountPrice=function(strPrice,strDiscount)
{
if (!dcs.common.isNumber(strDiscount)) strDiscount=100;
var re=Math.round((strPrice*strDiscount/100)/10)*10;
re=dcs.codes.toPrice(re);
return re;
}
Math.round(N)是Javascript的四舍五入函数,为了实现个位数的四舍五入,我们先将原来的数值除以10,四舍五入之后再乘以10就可以了。