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>

实现计算器的基本功能,希望能帮到你谢谢!记着点赞哦!

uniapp中实现简易计算器的更多相关文章

  1. C#Windows Form简易计算器实现(中)

    昨天花了一天的时间弄计算器.也算是做出来了,还是简易的(怀疑猿生!!).在此先感谢昨天被我骚扰的朋友. 先贴一张界面看看 其实健壮性还是挺差的,用户体验也是极差的.比如说用户输入了不合理运算式子,我就 ...

  2. 剖析简易计算器带你入门微信小程序开发

    写在前面,但是重点在后面 这是教程,也不是教程. 可以先看Demo的操作动图,看看是个什么玩意儿,GitHub地址(https://github.com/dunizb/wxapp-sCalc) 自从微 ...

  3. Python之实现一个简易计算器

    自己动手写计算器 一.功能分析 用户输入一个类似这样 3*( 4+ 50 )-(( 100 + 40 )*5/2- 3*2* 2/4+9)*((( 3 + 4)-4)-4) 这样的表达式,假设表达式里 ...

  4. C#Windows Form简易计算器实现(上)

    第一次写博客,来分享一个简易计算器的代码.作为一名准程序员,就是要多写代码才能孰能生巧.重视基础知识才能飞的更快更高以及更稳. 代码可能会写的很糟糕,不完美不安全之处希望发现的越多越好 c#编写计算器 ...

  5. JavaScript简易计算器

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...

  6. mini dc与简易计算器 20165235

    mini dc 任务内容 本次mini dc任务就是通过补充代码来实现整型数据的后缀表达式计算 相关知识 通过利用堆栈这一先进后出的数据结构来实现后缀表达式的计算.通过Stack<Integer ...

  7. 《Java 程序设计》课堂实践项目-简易计算器

    <Java 程序设计>课堂实践项目简易计算器 课后学习总结 目录 改变 简易计算器实验要求 课堂实践成果 课后思考 改变 修改了博客整体布局,过去就贴个代码贴个图很草率,这次布局和内容都有 ...

  8. 微信小程序-简易计算器

    代码地址如下:http://www.demodashi.com/demo/14210.html 一.前期准备工作 软件环境:微信开发者工具 官方下载地址:https://mp.weixin.qq.co ...

  9. 课堂限时训练-简易计算器·mini dc

    课堂限时训练-简易计算器·mini dc 实验题目 采用后缀表达式法,设计一个建议计算器,实现+.-.*./四种运算. 代码实现 码云链接 关键代码部分及结果如下: 实验分析 首先,分析一下后缀表达式 ...

  10. 用js制作简易计算器及猜随机数字游戏

    <!doctype html><html><head> <meta charset="utf-8"> <title>JS ...

随机推荐

  1. XAF新手入门 - 视图布局示例

    前言 掌握了应用程序模型(ApplicationModel)的基础知识之后,通过一个视图布局示例来加强对应用程序模型的理解. 官网给出了比较丰富的示例,并且这些示例涵盖了应用程序模型操作的方方面面,通 ...

  2. echart 带表格

    "echarts": "^5.2.2", ChartSituation1 <template> <div> <EChartTemp ...

  3. SVN迁移到Git,并同步提交记录

    原文:SVN迁移到Git,并同步提交记录 - Stars-One的杂货小窝 公司的旧项目存放在SVN,现准备迁移到Git,研究了下,简单记录一下从SVN迁移到Git的操作 步骤 1.创建一个空白文件夹 ...

  4. epoll和ractor的粗浅理解

    我们继续上篇的文章继续更新我们的代码. 首先就是介绍一下epoll的三个函数. epoll_create epoll_ctl epoll_wait 如何去理解这3个函数,我是这样去理解这个函数, 就像 ...

  5. sed第三天

    sed第三天 利用sed 取出ifconfIg ens33命令中本机的IPv4地址 可以百度扩展 了解即可 也可以用别的命令实现 只要有结果也可以 ifconfig ens33 | sed -n 's ...

  6. JS(简单数据类型、数据类型转换)

    一. 数据类型简介 1.1 为什么需要数据类型 在计算机中,不同的数据所需占用的存储空间是不同的,为了便于把数据分成所需内存大小不同的数据,充分利用存储空间,于是定义了不同的数据类型.简单来说,数据类 ...

  7. verilog注释及vscode插件terosHDL

    模型功能 实现代码的注释的方法 基于vscode的文档自动生成 模型框图 `timescale 1ns / 1ps /* */ // ********************************* ...

  8. proteus之四状态锁定器

    proteus之四状态锁定器 1.实验原理 利用4071(或门)的锁定功能,当输入为1时输出结果锁定为1,使结果锁定在这个地方.4028(BCD译码器)将输入转化为输出,利用输出反馈到或门用于自锁. ...

  9. Mac M芯片使用PD安装centos7无页面安装

    1.选择Centos镜像 点击继续 设置虚拟机名称: 点击创建 : 选择第一个回车开始下载系统,下载完成进入设置页面,首先输入 1 设置语言: 进入语言设置,选择77普通话: 选择c继续,又回到系统配 ...

  10. archlinux virtualbox 启用usb支持

    参照 https://linux.cn/article-15287-1.html 1.安装virtualbox扩展包 (1)从archlinuxcn社区库安装 sudo pacman -S virtu ...