当前位置:网站首页>JS four operations are repackaged to solve the problem of precision loss
JS four operations are repackaged to solve the problem of precision loss
2022-07-28 06:51:00 【__ Simon】
math.js
/* eslint-disable one-var */
/* eslint-disable no-extend-native */
/*
* @Author: Simon
* @Date: 2021-07-14 16:32:43
* @LastEditors: Simon
* @LastEditTime: 2022-04-11 18:49:53
* @Description: Four operations reseal , Solve the problem of precision loss
*/
// Add
Number.prototype.add = function (arg) {
let r1, r2, m
try { r1 = this.toString().split('.')[1].length } catch (e) { r1 = 0 }
try { r2 = arg.toString().split('.')[1].length } catch (e) { r2 = 0 }
m = Math.pow(10, Math.max(r1, r2))
return (this * m + arg * m) / m
}
// Subtraction
Number.prototype.sub = function (arg) {
let r1, r2, m, n
try { r1 = this.toString().split('.')[1].length } catch (e) { r1 = 0 }
try { r2 = arg.toString().split('.')[1].length } catch (e) { r2 = 0 }
m = Math.pow(10, Math.max(r1, r2))
// Dynamic control precision length
n = (r1 >= r2) ? r1 : r2
return Number(((this * m - arg * m) / m).toFixed(n))
}
// Multiplication
Number.prototype.mul = function (arg) {
let m = 0, s1 = this.toString(), s2 = arg.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)
}
// division
Number.prototype.div = function (arg) {
let t1 = 0, t2 = 0, r1, r2
try { t1 = this.toString().split('.')[1].length } catch (e) { }
try { t2 = arg.toString().split('.')[1].length } catch (e) { }
r1 = Number(this.toString().replace('.', ''))
r2 = Number(arg.toString().replace('.', ''))
return (r1 / r2) * Math.pow(10, t2 - t1)
}
// Three digit segmentation ( Thousands plus commas )
Number.prototype.toThousandsString = function () {
let str = this.toString()
let reg = ~str.indexOf('.') ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
return str.replace(reg, '$1,')
}
边栏推荐
- 测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
- 技术分享 | 使用postman发送请求
- 项目编译NoSuch***Error问题
- [pta-- use queues to solve the problem of monkeys choosing kings]
- About the collation of shader keyword
- JS四则运算重新封装,解决精度丢失问题
- Which brand of air conduction earphones is good and highly praised
- Initializingbean interface and examples
- iptables防火墙
- Skimming records -- sequence traversal of binary tree
猜你喜欢

Skimming records -- sequence traversal of binary tree

技术分享 | 使用 cURL 发送请求

项目编译NoSuch***Error问题

技术分享 | 接口测试价值与体系

Everything you don't know about time complexity is here

Which brand of air conduction earphones is better? These four should not be missed

iptables防火墙

Which is the best one to make air conduction headphones? Inventory of the best air conduction headphones

KVM hot migration

CentOS7部署MySQL数据库服务器
随机推荐
网络——数据链路层
Water bottle effect production
mysql索引优化
Redis cache design and performance optimization
测试面试题集锦(二)| 测试工具篇(附答案)
Lancher deployment practice
[explain in detail how to realize Sanzi chess step by step]
Question brushing record - linked list
[queue, simple application of stack ---- packaging machine]
测试面试题集锦(三)| 计算机网络和数据库篇(附答案)
[pta ---- traversal of tree]
KVM热迁移
[dynamic planning -- the best period for buying and selling stocks Series 2]
技术分享 | 接口测试价值与体系
rancher部署实战
Small tips
SSAO by computer shader (III)
Gerapy use
Which brand of air conduction earphones is better? These four should not be missed
Mongo SSL configuration practice