鸿 网 互 联 www.68idc.cn

javascript加减乘除的简单实例

来源:互联网 作者:佚名 时间:2016-08-05 02:29
下面小编就为大家带来一篇javascript加减乘除的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

javascript加减乘除的简单实例

<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <script language="javascript" type="text/javascript"> //除法函数 function accDiv(arg1,arg2){ var t1 = 0, t2 = 0, r1, r2, n; try { t1 = arg1.toString().split(".")[1].length; } catch(e) {t1 = 0;} try { t2 = arg2.toString().split(".")[1].length; } catch(e) {t2 = 0;} with(Math) { r1 = Number(arg1.toString().replace(".","")); r2 = Number(arg2.toString().replace(".","")); n = Math.max(t1,t2); return (r1/r2)*pow(10, t2-t1); } } //乘法函数 function accMul(arg1,arg2) { var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toString().split(".")[1].length; } catch(e) {t1 = 0;} try { t2 = arg2.toString().split(".")[1].length; } catch(e) {t2 = 0;} with(Math) { r1 = Number(arg1.toString().replace(".","")); r2 = Number(arg2.toString().replace(".","")); return (r1*r2)/pow(10, t2+t1); } } //加法函数 function accAdd(arg1,arg2){ var t1 = 0, t2 = 0, m; try { t1 = arg1.toString().split(".")[1].length; } catch(e) {t1 = 0;} try { t2 = arg2.toString().split(".")[1].length; } catch(e) {t2 = 0;} with(Math) { m=Math.pow(10,Math.max(t1,t2)); return (arg1 * m + arg2 * m) / m; } } //减法函数 function accSubtr(arg1,arg2){ var t1 = 0, t2 = 0, m, n; try { t1 = arg1.toString().split(".")[1].length; } catch(e) {t1 = 0;} try { t2 = arg2.toString().split(".")[1].length; } catch(e) {t2 = 0;} with(Math) { //动态控制精度长度 n = Math.max(t1,t2); m = Math.pow(10, n); //return (arg1 * m - arg2 * m) / m; return ((arg1 * m - arg2 * m) / m).toFixed(n); } } //给String类型增加一个div方法,调用起来更加方便。 String.prototype.div = function (arg){ return accDiv(this, arg); } //给String类型增加一个mul方法,调用起来更加方便。 String.prototype.mul = function (arg){ return accMul(arg,this); } //给String类型增加一个add方法,调用起来更加方便。 String.prototype.add = function (arg){ return accAdd(arg,this); } //给String类型增加一个subtr方法,调用起来更加方便。 String.prototype.subtr = function (arg){ return accSubtr(this, arg); } function cal() { var arg1 = document.Form1.TextBox1.value; var arg2 = document.Form1.TextBox2.value; //document.Form1.TextBox5.value = accDiv(arg1, arg2); //document.Form1.TextBox6.value = accMul(arg1, arg2); //document.Form1.TextBox7.value = accAdd(arg1, arg2); //document.Form1.TextBox8.value = accSubtr(arg1, arg2); document.Form1.TextBox5.value = arg1.div(arg2); document.Form1.TextBox6.value = arg1.mul(arg2); document.Form1.TextBox7.value = arg1.add(arg2); document.Form1.TextBox8.value = arg1.subtr(arg2); } </script> <body> <form method="post" runat="server"> <div> <div><input type="text" value="0" /></div> <div><input value="0" type="text" /></div> <div> <div>accDiv:<input type="text" /></div> <div>accMul:<input type="text" /></div> <div>accAdd:<input type="text" /></div> <div>accSubtr:<input type="text" /></div> </div> <div><input type="button" value="cal" /></div> </div> </form> </body> </html>

以上这篇javascript加减乘除的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

网友评论
<