You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.1 KiB
106 lines
3.1 KiB
7 months ago
|
|
||
|
// 判断传入的值-是否为整数
|
||
|
function isInteger(obj) {
|
||
|
return Math.floor(obj) === obj
|
||
|
}
|
||
|
|
||
|
// 将一个浮点数转成整数,返回整数和倍数。如 3.14 >> 314,倍数是 100
|
||
|
// @param floatNum { number } 小数
|
||
|
// @return { object }
|
||
|
// { times: 100, num: 314 }
|
||
|
|
||
|
// 用于返回整数和倍数
|
||
|
function toInteger(floatNum) {
|
||
|
// 声明一个对象用来保存倍数和整数
|
||
|
const ret = { times: 1, num: 0 }
|
||
|
// 第一种情况:是整数
|
||
|
if (isInteger(floatNum)) {
|
||
|
// 把整数给 ret中的 num
|
||
|
ret.num = floatNum
|
||
|
return ret // 最后返回 ret
|
||
|
}
|
||
|
|
||
|
// 第二种情况-不是整数,
|
||
|
const strfi = floatNum + '' // 转为字符串 "0.1"
|
||
|
const dotPos = strfi.indexOf('.') // 查询小数点
|
||
|
const len = strfi.substr(dotPos + 1).length; // 获取小数点后的长度
|
||
|
const times = Math.pow(10, len) // 放大多少倍
|
||
|
const intNum = Number(floatNum.toString().replace('.', '')) // 返回 转为字符串 截取掉小数点 最后转为数字(整数)
|
||
|
// 把获取到的倍数和整数存入对象中
|
||
|
ret.times = times
|
||
|
ret.num = intNum
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
|
||
|
// 核心方法,实现加减乘除运算,确保不丢失精度
|
||
|
// 思路:把小数放大为整数(乘),进行算术运算,再缩小为小数(除)
|
||
|
// @param a { number } 运算数1
|
||
|
// @param b { number } 运算数2
|
||
|
// @param digits { number } 精度,保留的小数点数,比如 2, 即保留为两位小数
|
||
|
// @param op { string } 运算类型,有加减乘除(add / subtract / multiply / divide)
|
||
|
|
||
|
|
||
|
|
||
|
function operation(a, b, digits, op) {
|
||
|
// 获取倍数和整数的对象
|
||
|
const o1 = toInteger(a)
|
||
|
const o2 = toInteger(b)
|
||
|
// 提取整数
|
||
|
const n1 = o1.num
|
||
|
const n2 = o2.num
|
||
|
// 提取倍数
|
||
|
const t1 = o1.times
|
||
|
const t2 = o2.times
|
||
|
|
||
|
// 获取最大倍数
|
||
|
const max = t1 > t2 ? t1 : t2
|
||
|
let result = 0 //
|
||
|
|
||
|
switch (op) {
|
||
|
case 'add':
|
||
|
if (t1 === t2) { // 两个小数位数相同
|
||
|
result = n1 + n2 //
|
||
|
} else if (t1 > t2) { // o1 小数位 大于 o2
|
||
|
result = n1 + n2 * (t1 / t2)
|
||
|
} else { // o1 小数位 小于 o2
|
||
|
result = n1 * (t2 / t1) + n2
|
||
|
}
|
||
|
return result / max
|
||
|
case 'subtract':
|
||
|
if (t1 === t2) {
|
||
|
result = n1 - n2
|
||
|
} else if (t1 > t2) {
|
||
|
result = n1 - n2 * (t1 / t2)
|
||
|
} else {
|
||
|
result = n1 * (t2 / t1) - n2
|
||
|
}
|
||
|
return result / max
|
||
|
case 'multiply':
|
||
|
result = (n1 * n2) / (t1 * t2)
|
||
|
return result
|
||
|
case 'divide':
|
||
|
result = (n1 / n2) * (t2 / t1)
|
||
|
return result
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 加减乘除的四个接口
|
||
|
export function add(a, b, digits) {
|
||
|
return operation(a, b, digits, 'add')
|
||
|
}
|
||
|
export function subtract(a, b, digits) {
|
||
|
return operation(a, b, digits, 'subtract')
|
||
|
}
|
||
|
export function multiply(a, b, digits) {
|
||
|
return operation(a, b, digits, 'multiply')
|
||
|
}
|
||
|
export function divide(a, b, digits) {
|
||
|
return operation(a, b, digits, 'divide')
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
// console.log(floatObj.add(0.5, 0.2))
|
||
|
// console.log(floatObj.add(0.12, 0.3))
|