微信小程序?qū)崿F(xiàn)計(jì)算器案例
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
項(xiàng)目展示
頁(yè)面設(shè)計(jì)
分為上面輸入的顯示部分和下面按鍵部分
<!--pages/index/index.wxml--> <view class="result"> <view class="result-num">{{num}}</view> <view class="result-op">{{op}}</view> </view> <view class="btns"> <view> <view hover-class="bg" bindtap="resetBtn">C</view> <view hover-class="bg" bindtap="delBtn">DEL</view> <view hover-class="bg" bindtap="opBtn" data-val="%">%</view> <view hover-class="bg" bindtap="opBtn" data-val="/">÷</view> </view> <view> <view hover-class="bg" bindtap="numBtn" data-val="7">7</view> <view hover-class="bg" bindtap="numBtn" data-val="8">8</view> <view hover-class="bg" bindtap="numBtn" data-val="9">9</view> <view hover-class="bg" bindtap="opBtn" data-val="*">×</view> </view> <view> <view hover-class="bg" bindtap="numBtn" data-val="4">4</view> <view hover-class="bg" bindtap="numBtn" data-val="5">5</view> <view hover-class="bg" bindtap="numBtn" data-val="6">6</view> <view hover-class="bg" bindtap="opBtn" data-val="-">-</view> </view> <view> <view hover-class="bg" bindtap="numBtn" data-val="1">1</view> <view hover-class="bg" bindtap="numBtn" data-val="2">2</view> <view hover-class="bg" bindtap="numBtn" data-val="3">3</view> <view hover-class="bg" bindtap="opBtn" data-val="+">+</view> </view> <view> <view hover-class="bg" bindtap="numBtn" data-val="0">0</view> <view hover-class="bg" bindtap="dotBtn">.</view> <view hover-class="bg" bindtap="opBtn" data-val="=">=</view> </view> </view>
頁(yè)面樣式
/* pages/index/index.wxss */ page { display: flex; flex-direction: column; height: 100%; color: #555; } .result { flex: 1; background: #f3f6fe; position: relative; } .result-num { position: absolute; font-size: 27pt; bottom: 5vh; right: 3vw; } .result-op { font-size: 15pt; position: absolute; bottom: 1vh; right: 3vw; } .btns { flex: 1; } /* 按鈕樣式 */ .bg { background: rgb(223, 44, 20); } .btns { flex: 1; display: flex; flex-direction: column; font-size: 17pt; border-top: 1rpx solid #ccc; border-left: 1rpx solid #ccc; } .btns > view { flex: 1; display: flex; } .btns > view > view { flex-basis: 25%; border-right: 1rpx solid #ccc; border-bottom: 1rpx solid #ccc; box-sizing: border-box; display: flex; align-items: center; justify-content: center; } .btns > view:last-child > view:first-child { flex-basis: 50%; } .btns > view:first-child > view:first-child { color: #f00; } .btns > view > view:last-child { color: #fc8e00; }
頁(yè)面邏輯
util–>calc.js
計(jì)算過(guò)程是將小數(shù)都乘以兩數(shù)10的最大次冪化為整數(shù),這樣可進(jìn)行高精度計(jì)算,最后再將得數(shù)除以相應(yīng)的10的次冪
例如
// 精確計(jì)算 module.exports = { // 加 add: 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 } // 將小數(shù)都化為整數(shù)在進(jìn)行計(jì)算 m是需要×的10的冪數(shù) m = Math.pow(10, Math.max(r1, r2)) // 最后返回的時(shí)候再除以m return (arg1 * m + arg2 * m) / m }, // 減 sub: 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)) //動(dòng)態(tài)控制精度長(zhǎng)度 n = (r1 >= r2) ? r1 : r2 return ((arg1 * m - arg2 * m) / m).toFixed(n) }, // 乘 mul: 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) }, // 除 div: 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) {} r1 = Number(arg1.toString().replace(".", "")) r2 = Number(arg2.toString().replace(".", "")) return (r1 / r2) * Math.pow(10, t2 - t1) } }
index.js
數(shù)字點(diǎn)擊處理事件
當(dāng)點(diǎn)擊數(shù)字不為零,并且指示不清空時(shí)候,將輸入的num拼接到page里的num
// 數(shù)字按鈕事件處理函數(shù) numBtn: function(e) { var num = e.target.dataset.val if (this.data.num === '0' || this.isClear) { this.setData({ num: num }) this.isClear = false } else { this.setData({ num: this.data.num + num }) } },
運(yùn)算符處理事件
// 運(yùn)算符事件處理函數(shù) opBtn: function(e) { var op = this.data.op // 獲取之前的數(shù) var num = Number(this.data.num) this.setData({ op: e.target.dataset.val }) if (this.isClear) { return } this.isClear = true if (this.result === null) { this.result = num return } if (op === '+') { this.result = calc.add(this.result, num) } else if (op === '-') { ...... 其他運(yùn)算操作(詳細(xì)代碼看下面完整代碼部分) ...... } this.setData({ num: this.result + '' }) },
全部js
// pages/index/index.js const calc = require('../../utils/calc.js') Page({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { num: '0', op: '' }, // 結(jié)果 result: null, // 是否清空數(shù)字行 /* 清空的情況(值為true) 點(diǎn)擊過(guò)運(yùn)算符之后,改為true 以便下一次輸入數(shù)字顯示 點(diǎn)擊清空 */ isClear: false, // 數(shù)字按鈕事件處理函數(shù) numBtn: function(e) { var num = e.target.dataset.val if (this.data.num === '0' || this.isClear) { this.setData({ num: num }) this.isClear = false } else { this.setData({ num: this.data.num + num }) } }, // 運(yùn)算符事件處理函數(shù) opBtn: function(e) { var op = this.data.op // 獲取之前的數(shù) var num = Number(this.data.num) this.setData({ op: e.target.dataset.val }) if (this.isClear) { return } this.isClear = true if (this.result === null) { this.result = num return } if (op === '+') { this.result = calc.add(this.result, num) } else if (op === '-') { this.result = calc.sub(this.result, num) } else if (op === '*') { this.result = calc.mul(this.result, num) } else if (op === '/') { this.result = calc.div(this.result, num) } else if (op === '%') { this.result = this.result % num } this.setData({ num: this.result + '' }) }, // 小數(shù)點(diǎn)事件處理函數(shù) dotBtn: function() { if (this.isClear) { this.setData({ num: '0.' }) this.isClear = false return } if (this.data.num.indexOf('.') >= 0) { return } this.setData({ num: this.data.num + '.' }) }, // DEL按鈕處理函數(shù) delBtn: function() { var num = this.data.num.substr(0, this.data.num.length - 1) this.setData({ num: num === '' ? '0' : num }) }, // C按鈕事件處理函數(shù) resetBtn: function() { this.result = null this.isClear = false this.setData({ num: '0', op: '' }) } })
案例下載:微信小程序?qū)崿F(xiàn)計(jì)算器案例
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript與asp.net(c#)互相調(diào)用方法
js與C#之間相互調(diào)用的一些方法2009-12-12JavaScript中的canvas?實(shí)現(xiàn)一個(gè)圓環(huán)漸變倒計(jì)時(shí)效果
這篇文章主要介紹了JavaScript中的canvas?實(shí)現(xiàn)一個(gè)圓環(huán)漸變倒計(jì)時(shí)效果,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09淺析JavaScript動(dòng)畫(huà)模擬拖拽原理
本文主要對(duì)JavaScript動(dòng)畫(huà)模擬拖拽原理進(jìn)行分析,步驟清晰,簡(jiǎn)短的文字,深入的理解。需要的朋友可以看下2016-12-12JS鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)方法
程序開(kāi)發(fā)人員可以使用一些簡(jiǎn)單的技術(shù)來(lái)改進(jìn)自己的代碼編寫(xiě)工作。你可以寫(xiě)一些函數(shù)來(lái)處理各種常見(jiàn)任務(wù),以節(jié)省時(shí)間;也可以改進(jìn)一下代碼的實(shí)現(xiàn)方式,比如你可以把方法的鏈?zhǔn)秸{(diào)用技術(shù)用到自己所寫(xiě)的JS庫(kù)中,把自己喜歡的方法串起來(lái)調(diào)用。2013-03-03H5實(shí)現(xiàn)仿flash效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了H5實(shí)現(xiàn)仿flash效果的實(shí)現(xiàn)代碼的相關(guān)資料,希望通過(guò)本文能幫助到大家,實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09JS數(shù)組方法push()、pop()用法實(shí)例分析
這篇文章主要介紹了JS數(shù)組方法push()、pop()用法,結(jié)合實(shí)例形式分析了JavaScript數(shù)組push()與pop()方法基本功能、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01Rxjs?中處理錯(cuò)誤和抓取錯(cuò)誤的代碼案例
這篇文章主要介紹了Rxjs?中怎么處理和抓取錯(cuò)誤,本文,我們學(xué)習(xí)了如何使用?catchError?在數(shù)據(jù)流中抓取錯(cuò)誤,怎么去修改和返回?observable,或者使用?EMPTY?不去觸發(fā)組件中的錯(cuò)誤,需要的朋友可以參考下2022-08-08