一、使用方法
1、在使用的页面引入
import { JSAdd,JSSub,JSDiv, JSMul } from '../../until/jsOperation'
1、对需要计算的数值
JSDiv(除数, 被除数)
如:10000/100
JSAdd(10000, 100)
JSSub(10000, 100)
JSMul(10000, 100)
JSDiv(10000, 100)
JSDiv(bargainAmountMin, 100)
JSMul()的用法亦如此
二、方法
1、加法
export function JSAdd (a, b) {
if (!a || !b) {
return a + b
}
let c, d
try {
c = a.toString().split('.')[1].length
} catch (f) {
c = 0
}
try {
d = b.toString().split('.')[1].length
} catch (f) {
d = 0
}
const h = Math.pow(10, Math.max(c, d))
return (JSMul(a, h) + JSMul(b, h)) / h
}
2、减法
export function JSSub (a, b) {
if (!a || !b) {
return a - b
}
let c, d
try {
c = a.toString().split('.')[1].length
} catch (f) {
c = 0
}
try {
d = b.toString().split('.')[1].length
} catch (f) {
d = 0
}
const h = Math.pow(10, Math.max(c, d))
return (JSMul(a, h) - JSMul(b, h)) / h
}
3、乘法
export function JSMul (a, b) {
if (!a || !b) {
return 0
}
let c = 0
const d = a.toString()
const e = b.toString()
try {
if (d.split('.')[1]) {
c += d.split('.')[1].length
}
} catch (f) {
console.log(f)
}
try {
if (e.split('.')[1]) {
c += e.split('.')[1].length
}
} catch (f) {
console.log(f)
}
const result =
(Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c)
return result
}
4、除法
export function JSDiv (a, b) {
if (!a || !b) {
return 0
}
let e = 0
let f = 0
try {
if (a.toString().split('.')[1]) {
e = a.toString().split('.')[1].length
}
} catch (g) {
console.log(g)
}
try {
if (b.toString().split('.')[1]) {
f = b.toString().split('.')[1].length
}
} catch (g) {
console.log(g)
}
const result = JSMul(
Number(a.toString().replace('.', '')) /
Number(b.toString().replace('.', '')),
Math.pow(10, f - e)
)
return result
}