uniapp中實現簡易計算器


uniapp中實現簡易計算器
主要問題:在計算器的實現過程中會遇到小數點計算精度;
此計算器是依賴了uni-popup的彈出層插件,可在uniapp官方組件中查找擴展插件popup彈窗層下載,也可直接點擊該(https://ext.dcloud.net.cn/plugin?id=329)鏈接直接下載

計算器效果圖

話不多說上代碼:

HTML源碼

<template>
    <view class="uni-popup-calculator">
        <view class="uni-popup-calculator-title">
            <text>{{number}}</text>
        </view>
        <view class="uni-popup-content">
            <view class="uni-popup-cell" :class="[item.flag?'active':'',item.width == 2?'cur':'',item.border?'border':'']"
             @click.stop="calculationTwo(item)" v-for="(item,idx) in calculator" :key="idx">
                {{item.name}}
            </view>
        </view>
    </view>
</template>

Js源碼

<script>
    export default {
        name: 'UniPopupCalculator',
        inject: ['popup'],
        data() {
            return {
                number: '0',//計算器顯示區域值
                calculator: [{ //計算器各按鍵
                    name: 'c',
                    flag: false,
                    type: 'clear',
                    width: 2,
                    border: true
                }, {
                    name: '%',
                    flag: false,
                    type: 'operator',
                    width: 1,
                    border: false
                }, {
                    name: '/',
                    flag: false,
                    type: 'operator',
                    width: 1,
                    border: false
                }, {
                    name: '7',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: true
                }, {
                    name: '8',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: false
                }, {
                    name: '9',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: false
                }, {
                    name: '*',
                    flag: false,
                    type: 'operator',
                    width: 1,
                    border: false
                }, {
                    name: '4',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: true
                }, {
                    name: '5',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: false
                }, {
                    name: '6',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: false
                }, {
                    name: '+',
                    flag: false,
                    type: 'operator',
                    width: 1,
                    border: false
                }, {
                    name: '1',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: true
                }, {
                    name: '2',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: false
                }, {
                    name: '3',
                    flag: false,
                    type: 'number',
                    width: 1,
                    border: false
                }, {
                    name: '-',
                    flag: false,
                    type: 'operator',
                    width: 1,
                    border: false
                }, {
                    name: '0',
                    flag: false,
                    type: 'number',
                    width: 2,
                    border: true
                }, {
                    name: '.',
                    flag: false,
                    type: 'string',
                    width: 1,
                    border: false
                }, {
                    name: '=',
                    flag: false,
                    type: 'equal',
                    width: 1,
                    border: false
                }],
                numberOne: '',//變量一
                numberTwo: '',//變量二
                symbol: '',//運算符
                complete: false,//判斷是否完成一次計算
                current: -1
            }
        },
        created() {},
        methods: {
            //計算器方法二:
            calculationTwo: function(item) {
                let that = this;
                //按鍵點擊效果
                item.flag = true;
                setTimeout(() => {
                    item.flag = false;
                }, 200);
                //判斷輸入的第一位是否是小數點
                switch (item.type) {
                    case 'string': //小數點
                        if (that.complete) {
                            that.number = '0';
                            that.complete = false;
                        }
                        if (that.symbol) {
                            if ((that.number).indexOf('.') == -1) {
                                that.numberTwo = that.numberTwo + '.';
                                that.number = that.numberTwo;
                            }
                        } else {
                            if ((that.number).indexOf('.') == -1) {
                                that.number = that.number + '.';
                            }
                        }
                        break;
                    case 'number': //數字
                        if (that.complete) {
                            that.number = '0';
                            that.complete = false;
                        }
                        if (that.symbol) {
                            that.numberTwo += item.name;
                            that.number = that.numberTwo;
                        } else {
                            if (that.number == '0') {
                                that.number = item.name;
                            } else {
                                that.number += item.name;
                            }
                        }
                        break;
                    case 'operator': //運算符
                        that.symbol = item.name;
                        if (item.name != '%') {
                            that.numberOne = that.number;
                            that.numberTwo = '';
                        } else {
                            that.number = parseFloat(that.number) / 100;
                        }
                        break;
                    case 'equal': //等號
                        if (that.symbol == '-') {
                            that.number = that.subtraction(that.numberOne, that.numberTwo);
                        } else if (that.symbol == '+') {
                            that.number = that.addition(that.numberOne, that.numberTwo);
                        } else if (that.symbol == '*') {
                            that.number = that.multiplication(that.numberOne, that.numberTwo);
                        } else if (that.symbol == '/') {
                            that.number = that.division(that.numberOne, that.numberTwo);
                        } else if (that.symbol == '%') {
                            that.number = parseFloat(that.number) / 1;
                        }
                        that.complete = true;
                        that.numberOne = '';
                        that.numberTwo = '';
                        that.symbol = '';
                        break;
                    case 'clear': //清除符
                        that.clear();
                        break;
                }
            },
            /**
             * 清除
             * */
            clear: function() {
                let that = this;
                that.number = '0';
                that.numberOne = '';
                that.numberTwo = '';
                that.symbol = '';
                that.compile = false;
            },
            /**
             * 除法
             * */
            division: function(arg1, arg2) {
                var t1 = 0,
                    t2 = 0,
                    r1, r2;
                try {
                    t1 = arg1.toString().split(".")[1].length
                } catch (e) {}
                try {
                    t2 = arg2.toString().split(".")[1].length
                } catch (e) {}
                Math.r1 = Number(arg1.toString().replace(".", ""))
                Math.r2 = Number(arg2.toString().replace(".", ""))
                return (Math.r1 / Math.r2) * Math.pow(10, t2 - t1);
            },
            /**
             * 乘法
             * */
            multiplication: function(arg1, arg2) {
                var m = 0,
                    s1 = arg1.toString(),
                    s2 = arg2.toString();
                try {
                    m += s1.split(".")[1].length
                } catch (e) {}
                try {
                    m += s2.split(".")[1].length
                } catch (e) {}
                return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
            },
            /**
             * 加法
             * */
            addition: function(arg1, arg2) {
                var r1, r2, m;
                try {
                    r1 = arg1.toString().split(".")[1].length;
                } catch (e) {
                    r1 = 0;
                }
                try {
                    r2 = arg2.toString().split(".")[1].length;
                } catch (e) {
                    r2 = 0;
                }
                m = Math.pow(10, Math.max(r1, r2));
                return (arg1 * m + arg2 * m) / m;
            },
            /**
             * 減法
             * */
            subtraction: function(arg1, arg2) {
                var r1, r2, m, n;
                try {
                    r1 = arg1.toString().split(".")[1].length;
                } catch (e) {
                    r1 = 0;
                }
                try {
                    r2 = arg2.toString().split(".")[1].length;
                } catch (e) {
                    r2 = 0;
                }
                m = Math.pow(10, Math.max(r1, r2));
                //last modify by deeka
                //動態控制精度長度
                n = (r1 >= r2) ? r1 : r2;
                return ((arg1 * m - arg2 * m) / m).toFixed(n);
            }
        }
    }
</script>

Css源碼

<style lang="less">
    .uni-popup-calculator {
        background-color: #333333;
    }

    .uni-popup-calculator-title {
        width: 702rpx;
        height: 120rpx;
        line-height: 120rpx;
        padding: 0 24rpx;
        text-align: right;
        background-color: #333333;
        font-size: 50rpx;
        font-weight: 600;
        color: #FFFFFF;
    }

    .uni-popup-content {
        width: 750rpx;
        background-color: #FFFFFF;
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: flex-start;
        flex-wrap: wrap;

        .uni-popup-cell {
            width: 186rpx;
            height: 98rpx;
            line-height: 98rpx;
            text-align: center;
            font-size: 44rpx;
            font-weight: 600;
            color: #333333;
            border-bottom: 1px solid #f5f5f5;
            border-left: 1px solid #f5f5f5;
        }

        .uni-popup-cell.cur {
            width: 372rpx;
        }

        .uni-popup-cell.border {
            border-left: none;
        }

        .uni-popup-cell.active {
            background-color: #f5f5f5;
        }
    }
</style>

實現計算器的基本功能,希望能幫到你謝謝!記着點贊哦!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM