Avoid scientific notation in the numerical output of mathematical calculations of decimal numbers in javascript

2.9k Views Asked by At

I have a function that performs mathematical calculations on various numbers, including decimal numbers, and returns two string and numeric outputs. The problem I have with numeric output for decimal numbers is that it is a scientific notation. After several hours of searching the internet and seeing similar questions on stackoverflow and github, I finally came up with a combination of several functions to get to a point where the string output is smooth and accurate. But numerical output is still a problem and the result is a scientific notation.

so I decided to ask a question here, maybe I can find help for my problem

now consider sending the two numbers 2.5183213 and 2.518321 with the subtraction sign and the number of 8 decimal places to the main function of the following code snippet

the string output will be 0.0000003 and the numeric output will be 3e-7. While the numerical output should be equal to 0.0000003 and of numerical type

snippet sample

// 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>

0

There are 0 best solutions below