0

我有一个函数可以对各种数字(包括十进制数)执行数学计算,并返回两个字符串和数字输出。我对十进制数的数字输出的问题是它是一种科学记数法。在网上搜索了几个小时,在stackoverflow和github上看到了类似的问题,我终于想出了几个函数的组合,达到了字符串输出流畅准确的地步。但是数字输出仍然是一个问题,结果是科学计数法。

所以我决定在这里问一个问题,也许我可以为我的问题找到帮助

现在考虑将带减号和小数点后 8 位的两个数字 2.5183213 和 2.518321 发送到以下代码片段的主函数

字符串输出为 0.0000003,数字输出为 3e-7。虽然数字输出应等于 0.0000003 并且是数字类型

片段样本

// main function
function decFix(num1, num2, operation, lentDec = 8, outType = "number") {
    num1 = Number(num1).toFixed(lentDec);
    num2 = Number(num2).toFixed(lentDec);
    let result = 0

    if (operation == "+") {
        if (outType === "string") {
            result = `${toFixed(Number((num1 + num2).toFixed(lentDec)))}`
        } else {
            result = Number((num1 + num2).toFixed(lentDec))
        }
    } else if (operation === "-") {
        if (outType === "string") {
            result = `${toFixed(Number((num1 - num2).toFixed(lentDec)))}`
        } else {
            result = Number((num1 - num2).toFixed(lentDec))
        }
    } else if (operation === "/") {
        if (outType === "string") {
            result = `${toFixed(Number((num1 / num2).toFixed(lentDec)))}`
        } else {
            result = Number((num1 / num2).toFixed(lentDec))
        }
    } else if (operation === "*") {
        if (outType === "string") {
            let multiplication = toFixed(Number((num1 * num2).toFixed(lentDec)))
            if (countDecimals(multiplication) > lentDec) {
                result = `${Number(multiplication).toFixed(lentDec)}`
            } else {
                result = `${multiplication}`
            }
        } else {
            result = Number((num1 * num2).toFixed(lentDec))
        }
    }
    return result
}

// count the number of available decimals
let countDecimals = function (value) {
    if (Math.floor(value) !== value)
        return value.toString().split(".")[1].length || 0;
    return 0;
}

// convert scientific notation numbers to normal numbers (output type will be string)
function toFixed(x) {
    if (Math.abs(x) < 1.0) {
        let e = parseInt(x.toString().split('e-')[1]);
        if (e) {
            x *= Math.pow(10, e - 1);
            x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
        }
    } else {
        let e = parseInt(x.toString().split('+')[1]);
        if (e > 20) {
            e -= 20;
            x /= Math.pow(10, e);
            x += (new Array(e + 1)).join('0');
        }
    }
    return x;
}



let a = 2.5183213
let b = 2.518321

let c = decFix(a, b, "-", 8, "string")
let d = decFix(a, b, "-", 8, "number")
let e = a - b

document.querySelector('#stringOutPut').innerHTML = `decFix string output: ${c} - type: ${typeof c}`
document.querySelector('#numericOutPut').innerHTML = `decFix numeric output: ${d} - type: ${typeof d}`
document.querySelector('#normalOutPut').innerHTML = `normal subtraction output: ${e}`
<div id="stringOutPut"></div>
----------------------------
<div id="numericOutPut"></div>
----------------------------
<div id="normalOutPut"></div>

4

0 回答 0