js common calculation

Due to the problem of calculation accuracy, for example, 0.1 + 0.2 = 0.3000000000000004, the following methods need to be sorted out to facilitate simple calculation.

The main idea is to convert to integer first, then to calculate, and then to return to floating point number after calculation

Getting decimal places and moving decimal places to the right are tool functions for converting to integers during calculation. Four functions of addition, subtraction, multiplication and division are used

Get decimal places

// Get decimal places
export function getDecimalPlace(num) {
    try {
        let result = num.toString().split('.')[1].length;
        return result;
    } catch (e) {
        return 0;
    }
}

Move decimal places to the right

/**
 * Move decimal right
 * @param movePlace Move steps, positive right, negative left
 */
export function moveDecimalPlace(num, movePlace) {
    let decimalPlace = getDecimalPlace(num);
    let step = movePlace - decimalPlace;

    // In order to deal with the problem that 268.34 * 100 is equal to 26833.99999996, we first convert it to integer type and then determine how to move it
    let intNum = Number(num.toString().replace('.', ''));
    if (step > 0) {
        return intNum * Math.pow(10, step);
    } else if (step < 0) {
        return intNum / Math.pow(10, -step);
    } else {
        return intNum;
    }
}

Here, to move the decimal point to the right, you need to first convert it to an integer, and then further process it because there are some floating-point numbers multiplied by 10 times, which will also cause problems!
For example: 268.34 * 100 = > 26833.9999999996

addition

/**
 * Add arg1 + arg2
 */
export function add(arg1, arg2) {
    let step1 = getDecimalPlace(arg1);
    let step2 = getDecimalPlace(arg2);

    let maxStep = Math.max(step1, step2);

    arg1 = moveDecimalPlace(arg1, maxStep);
    arg2 = moveDecimalPlace(arg2, maxStep);

    return (arg1 + arg2) / Math.pow(10, maxStep);
}

subtraction

/**
 * Subtraction arg1 - arg2
 */
export function sub(arg1, arg2) {
    return add(arg1, -arg2);
}

multiplication

/**
 * Multiply arg1 * arg2
 */
export function multiply(arg1, arg2) {
    let step1 = getDecimalPlace(arg1);
    let step2 = getDecimalPlace(arg2);

    let maxStep = Math.max(step1, step2);

    arg1 = moveDecimalPlace(arg1, maxStep);
    arg2 = moveDecimalPlace(arg2, maxStep);

    if (maxStep > 0) {
        let stepPow = Math.pow(10, maxStep);
        return (arg1 * arg2) / (stepPow * stepPow);
    }
    return arg1 * arg2;
}

division

/**
 * Division arg1 / arg2
 */
export function division(arg1, arg2) {
    let step1 = getDecimalPlace(arg1);
    let step2 = getDecimalPlace(arg2);

    let maxStep = Math.max(step1, step2);

    arg1 = moveDecimalPlace(arg1, maxStep);
    arg2 = moveDecimalPlace(arg2, maxStep);

    return arg1 / arg2;
}

Other

Source link

More code snippets

Personal blog links

Keywords: Javascript

Added by calabiyau on Tue, 03 Dec 2019 08:25:57 +0200