Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write the code for WeChat Mini Programs to realize the calculator function

Shulou Source: shulou.com Published: 2022-06-01 01:27:30 10月25日 Update

This article introduces the relevant knowledge of "how to write the code for WeChat Mini Programs to achieve calculator function". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The specific code is as follows:

Wxml

Backspace clear screen ❤ + 9 87-6 54 * 3 21 / 0. History =

Wxss

Input {width: 95%; height: 250rpx; margin: 0 auto; margin-bottom: 20rpx; border-bottom: 1rpx solid # ccc;}. Box {display: flex;} button {width: 20%; height: 150rpx; margin-bottom: 20rpx; line-height: 150rpx; background-color:rgb (0150250); color: white;}. Yellow-color {background-color:rgb (247,142,24)}

JS

/ / index.js// gets the application instance const app = getApp () Page ({data: {calculation: ", result:0, character: [], / / operational symbol operand: [], / / numeric temp:false}, / / input box input data add:function (e) {let input = e.currentTarget.dataset.text.var that = this If (input = = "+" | | input = = "-" | | input = "*" | input = = "/") {this.data.temp = false; / / is used to record whether the operator var item = "character [" + this.data.character.length+ "]"; this.setData ({[item]: input})} else {var item = "operand [" + this.data.operand.length+ "]" If (that.data.temp) {/ / get the previous value var res = "operand [" + (this.data.operand.length-1) + "]" var ress= that.data.operand.length-1; var xyz = that.data.operand [ress] * 10 + parseInt (input) That.setData ({[res]: xyz})} else {input = parseInt (input); that.data.temp = true That.setData ({[item]: input})} / / put all the content in the display box this.setData ({calculation:this.data.calculation+input})}, / / calculate the answer res:function () {console.log (this.data.character.length) Console.log (this.data.operand.length) var character_len = this.data.character.length; var operand_len = this.data.operand.length; console.log (operand_len-character_len) if (operand_len-character_len = = 1) {this.data.result = this.data.operand [0]; console.log ("initial value" + this.data.result); for (var item0

Tags: Input code content functions programs calculators numbers data more knowledge symbols operations utility learning and then history dilemmas examples reality screen Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn OPPO Reno macOS Docker Redmi