微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

避免在javascript中十进制数的数学计算的数值输出中使用科学记数法

如何解决避免在javascript中十进制数的数学计算的数值输出中使用科学记数法

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

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

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

字符串输出为 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,"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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。