js计算器封装

js计算器精度计算代码封装:

/**
 * Created by niuyadong on 2018/5/8.
 * 首先对数字进行校验。
 */
var digitalOperation = function(num){
    var regexStr= /^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/;
    var regexNum = /^-?[1-9]\d*$/;
    this.regexStr1 = regexStr;
    this.regexStr2 = regexNum;
    if(this.regexStr1.test(num)||this.regexStr2.test(num)){
        this.num = parseFloat(num);
    }else{
        this.num = parseFloat("0.00");
    }
    this.result = "0.00";
};
digitalOperation.prototype = {
    init:function () {

    },
    add:function (num2,digit) {
        var digit2 = 2;
        if(digit){
            digit2 = digit;
        }
        if(this.regexStr1.test(num2)||this.regexStr2.test(num2)){
            this.result = (this.num+parseFloat(num2)).toFixed(digit2);
        }
        return this.result;
    },
    subtract:function (num2,digit) {
        var digit2 = 2;
        if(digit){
            digit2 = digit;
        }
        if(this.regexStr1.test(num2)||this.regexStr2.test(num2)){
            this.result = (this.num-parseFloat(num2)).toFixed(digit2);
        }
        return this.result;
    },
    multiply:function (num2,digit) {
        var digit2 = 2;
        if(digit){
            digit2 = digit;
        }
        if(this.regexStr1.test(num2)||this.regexStr2.test(num2)){
            this.result = (this.num*parseFloat(num2)).toFixed(digit2);
        }
        return this.result;
    },
    divide:function(num2,digit){
        var digit2 = 2;
        if(digit){
            digit2 = digit;
        }
        if((this.regexStr1.test(num2)||this.regexStr2.test(num2))&&num2!=0){
            this.result = (this.num/parseFloat(num2)).toFixed(digit2);
        }
        return this.result;
    }
};

调用实例:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title>
    <script type="text/javascript" src="${ctx}/public/javascripts/test/digital_operation.js">script>
    <script type="text/javascript" src="${ctx}/public/javascripts/jQuery.js">script>
head>
<body>
<div style="width:100%;height:100%;">
    <form id="fm" method="post" action="" onsubmit="return false;" style="margin-top:4%;">
        <div class="me-div">
            <span class="spanfont" class="veryTop">数字1:span>
            <input type="text" id="num1" name="num1" size="25"/>
        div>
        <div class="me-div">
            <span class="spanfont" class="veryTop">数字2:span>
            <input type="text" id="num2" name="num2" size="25"/>
        div>
        <div class="me-div">
            <span class="spanfont" class="veryTop">精度:span>
            <input type="text" id="digit" name="digit" size="25"/>
        div>
        <div class="me-div">
            <span class="spanfont" class="veryTop">结果:span>
            <input type="text" id="result" name="result" size="25"/>
        div>
    form>
    <button id="cardBind" type="button">计算button>
div>

<script>
    $("#cardBind").click(function () {
       var digital = new digitalOperation($("#num1").val());
       var digit = new Number($("#digit").val());
       console.log(digital.add($("#num2").val()));
       console.log(digital.subtract($("#num2").val()));
       console.log(digital.multiply($("#num2").val(),digit));
       console.log(digital.divide($("#num2").val(),digit));
    });
script>

body>
html>

需要传入数字,和精度。会计算出填写精度的结果。需要引入jquery

你可能感兴趣的:(jsp)