zoukankan      html  css  js  c++  java
  • 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 id="Form1" name="Form1" method="post" runat="server">
    <div style="border:solid 1px #000000; 600px;">
    <div style="float:left; 30%;"><input id="TextBox1" type="text" value="0" name="TextBox1" /></div>
    <div style="float:left; 30%;"><input id="TextBox2" value="0" type="text" name="TextBox2" /></div>
    <div style="float:left; 30%;">
    <div>accDiv:<input id="TextBox5" type="text" name="TextBox5" /></div>
    <div>accMul:<input id="TextBox6" type="text" name="TextBox6" /></div>
    <div>accAdd:<input id="TextBox7" type="text" name="TextBox7" /></div>
    <div>accSubtr:<input id="TextBox8" type="text" name="TextBox8" /></div>
    </div>
    <div style="float:right; 10%;"><input type="button" name="aa" value="cal" onclick="cal();" /></div>
    </div>
    </form>
    </body>
    </html>
  • 相关阅读:
    正经学C#_循环[do while,while,for]:[c#入门经典]
    Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常
    asp.net core 获取当前请求的url
    在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be
    用orchard core和asp.net core 3.0 快速搭建博客,解决iis 部署https无法登录后台问题
    System.Data.Entity.Core.EntityCommandExecution The data reader is incompatible with the specified
    初探Java设计模式3:行为型模式(策略,观察者等)
    MySQL教程77-CROSS JOIN 交叉连接
    MySQL教程76-HAVING 过滤分组
    MySQL教程75-使用GROUP BY分组查询
  • 原文地址:https://www.cnblogs.com/diony/p/2015912.html
Copyright © 2011-2022 走看看