当前位置:网站首页>js 实现两个小数的相乘、相除、相加和相减功能
js 实现两个小数的相乘、相除、相加和相减功能
2022-08-07 01:40:00 【蓝天白裙少女】
<script type="text/javascript">
//两数相加
function numadd(a, b) {
a = isNaN(a) ? 0 : a
b = isNaN(b) ? 0 : b
var c, d, e;
try {
c = a.toString().split(".")[1].length;
} catch (f) {
c = 0;
}
try {
d = b.toString().split(".")[1].length;
} catch (f) {
d = 0;
}
return e = Math.pow(10, Math.max(c, d)), (nummul(a, e) + nummul(b, e)) / e;
}
//两数相乘
function nummul(a, b) {
a = isNaN(a) ? 0 : a
b = isNaN(b) ? 0 : b
var c = 0,
d = a.toString(),
e = b.toString();
try {
c += d.split(".")[1].length;
} catch (f) {
f == f
}
try {
c += e.split(".")[1].length;
} catch (f) {
f == f
}
return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
}
//两数相减
function numsub(a, b) {
a = isNaN(a) ? 0 : a
b = isNaN(b) ? 0 : b
var c, d, e;
try {
c = a.toString().split(".")[1].length;
} catch (f) {
c = 0;
}
try {
d = b.toString().split(".")[1].length;
} catch (f) {
d = 0;
}
return e = Math.pow(10, Math.max(c, d)), (nummul(a, e) - nummul(b, e)) / e;
}
/** * 两数相除 * @param a 被除数 * @param b 除数 * @returns {*} */
function numdiv(a, b) {
a = isNaN(a) ? 0 : a
b = isNaN(b) ? 0 : b
var c, d, e = 0,
f = 0;
try {
e = a.toString().split(".")[1].length;
} catch (g) {
g == g
}
try {
f = b.toString().split(".")[1].length;
} catch (g) {
g == g
}
return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), nummul(c / d, Math.pow(10, f - e));
}
$(function(){
alert(numadd(1.414,1.732));
alert(nummul(1.732,1.4146));
alert(numsub(1.414,1.732));
alert(numdiv(1.732,1.4146));
});
</script>
边栏推荐
猜你喜欢

Use latex language to write formulas in word,

双端链表LinkedList

HTB-Valentine

文献阅读(183)MAGMA

LeetCode:每日一题【第一周】

ECCV2022 | FPN错位对齐,实现高效半监督目标检测 (PseCo)

Go-Excelize API源码阅读(九)——SetSheetBackground(sheet, picture string)

【8.5】代码源 - 【GCD】【序列中位数】【最大连边数量】

登顶KITTI!Mix-Teaching:适用于单目3D目标检测的半监督方法【清华大学】

搭建Lua开发环境(Lua解释器+IDEA编译器)
随机推荐
黑马2022最新redis课程笔记知识点(面试用)持续更新
【软件工具使用】【Latex快速上手】VSCode + MiKTeX + SmutraPDF(含正向搜索+反向搜索)
Speech recognition and conversion small test knife (1)
[Notes] Single test calls local interface
【Verilog基础】DFT(Design for Test)可测性设计的一些基础概念
【LeetCode】300.最长递增子序列
Error when connecting to MySQL: 2059 - authentication plugin 'caching_sha2_password' cannot be loaded...
习题:选择结构(二)
1323_STM32F103_ADC test
cmd 命令查询域名的 dns 解析记录(A,NS,MX,CNAME,TXT)
论文解读《PCT: Point cloud transformer》
GreenPlum 实现伪自治事务
HCIP实验(08)
Understanding of data type float storage structure
P4315 月下“毛景树”(树链剖分)
English Grammar_Prepositions - Table Place
mysqlworkbench连接方式
Introduction to Ftrace function graph
华为云数据库,更安全、更方便、更省钱的选择【华为云至简致远】
宅基地不能买卖,永久租赁可行吗