微信小程序:计算器(附源码)

这篇具有很好参考价值的文章主要介绍了微信小程序:计算器(附源码)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

效果图:

微信小程序:计算器(附源码)

代码实现:

index.js页面:(需要引入一个精确计算)

//引入精确计算的js
const acc = require('../../utils/accurate.js');

Page({
  data: {
    num:'0',
    op:''
  },
  result : null,
  isClear : false,
  numBtn(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});
     }
  },
  opBtn(e){
    var op = this.data.op;
    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 = acc.add(this.result, num);
    } else if(op === '-'){
      this.result = acc.sub(this.result, num);
    } else if(op === '*'){
      this.result = acc.mul(this.result, num);
    } else if(op === '/'){
      this.result = acc.div(this.result, num);
    } else if(op === '%'){
      this.result = this.result % num;
    }
    this.setData({num : this.result + ''});
  },
  dotBtn(e){
    if (this.isClear){
      this.setData({num : '0.'});
      this.isClear = false;
      return;
    }
    if (this.data.num.indexOf('.') >= 0){
      return;
    }
    this.setData({ num : this.data.num + '.'});
  },
  delBtn(e){
    var num = this.data.num.substr(0,this.data.num.length - 1);
    this.setData({ num : num === '' ? '0' : num});
  },
  resetBtn(e){
    this.result = null;
    this.isClear = false;
    this.setData({num : '0',op : ''});
  }
})

精确计算的JS:accurate.js

module.exports = {
  add(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;
  },
  sub(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));
    n = (r1 >= r2)? r1:r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
  },
  mul(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(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.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="*">x</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>

index.wxss页面:

page {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.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;
  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;
}
.bg {
  background: #eee;
}

index.json:

{
  "navigationBarBackgroundColor": "#fff",
  "navigationBarTitleText": "计算器",
  "navigationBarTextStyle": "black"
}

源码包:

百度网盘链接:https://pan.baidu.com/s/1PTXK0t44kRJ5onVk9leROw
提取码:4d2k文章来源地址https://www.toymoban.com/news/detail-511973.html

到了这里,关于微信小程序:计算器(附源码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序-简易计算器

    微信小程序-简易计算器,满足日常所用的的加减乘除计算 一、前期准备工作 软件环境:微信开发者工具 官方下载地址:微信开发者工具下载地址与更新日志 | 微信开放文档 1、基本需求。 简易计算器 满足日常所用的的加减乘除计算 带历史记录,查看过往计算 2、案例目录结构

    2024年02月04日
    浏览(34)
  • 微信小程序——简单计算器

    实现代码: computer.json 欢迎大家阅读,本人见识有限,写的博客难免有错误或者疏忽的地方,还望各位指点,在此表示感激不尽。文章持续更新中…

    2024年02月06日
    浏览(30)
  • 微信小程序3-2 计算器

    1、创建项目 创建项目微信小程序项目 - 计算器 单击【确定】按钮 按照惯例做一些初始化工作 2、设置导航栏 在 app.json 文件设置 window 配置项 pages/index/index.wxml 文件 1、编写页面的整体结构 2、编写结果区域的结构 两行内容:第一行是当前的计算式,第二行是当前计算结果

    2024年04月26日
    浏览(33)
  • 微信小程序如何制作简易计算器

    1、首先在浏览器中输入\\\"GitHub - dunizb/wxapp-sCalc: :speech_balloon:微信小程序版简易计算器demo,适合入门练手\\\"网址,输入完成后会进入到以下这个界面: 2、点击旁边绿色的按键 3、点击之后会有以下几个选项: 点击最后一行的Download ZIP,下载一个文件 然后解压一下这个文件,解压完

    2024年04月23日
    浏览(47)
  • 微信小程序案例3-2:计算器

    录屏演示多次计算 1、创建项目 创建微信小程序项目 - 计算器 单击【确定】按钮 按照惯例,做一些初始化工作 2、设置导航栏 在 app.json 设置 window 配置项 pages/index/index.wxml 文件 1、编写页面整体结构 2、编写结果区域结构 两行内容:第一行是当前的计算式,第二行是当前计算

    2024年04月12日
    浏览(71)
  • 微信小程序如何写一个计算器

    构思:1.画一个计算界面            2.找一个计算器实现库            3.调用实现库进行计算 实现:1.画出计算器图标、界面  页面代码实现 样式代码 实现代码 库代码  

    2024年02月11日
    浏览(31)
  • 房贷计算器微信小程序原生语言

    效果: 输入 300万 结果 还款明细 一共有3个页面 1、输入页面 2、结果页面 3、详情页面 index.wxml文件

    2024年04月28日
    浏览(45)
  • 微信小程序入门项目-做一个马马虎虎的计算器

    效果: 打开微信开发者工具,新建小程序 打开app.json设置顶部的标题和颜色 由于小程序不支持JavaScript的一些函数,比如evel(),所以代码变得复杂很多。 pagesindexindex.js的代码:

    2024年02月08日
    浏览(38)
  • uniapp写一个计算器用于记账(微信小程序,APP)

    提要:自己用uniapp写了一个记账小程序(目前是小程序),写到计算器部分,在网上找了别人写的计算器,大多数逻辑都是最简单的,都不能满足一个记账计算器的基本逻辑。与其在网上找来找去,还不如自己多捣鼓捣鼓。 https://gitee.com/yapplee/kog-calculator.git 贴部分主要代码,

    2024年02月05日
    浏览(36)
  • QT实现简单计算器(附源码)

    作为qt初学者,自己做一个简单计算器是比较普遍的练习题,使用widget方式实现一个简单的计算器功能。 使用Push Button组件将ui界面布局 显示界面使用Line Edit组件 将各个组件槽函数关联 等于符号的槽函数(进行加减乘除运算) 成功调试

    2024年02月12日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包