微信小程序

js,Ts 数字的精度计算

2020-12-22  本文已影响0人  Morbid_D

前端计算对后端返回的数据进行计算时,就会出现1.333333333类似的数据,这是因为js的精度计算并没有达到一定的要求。因为自己也出现了这样的问题,所以去寻找了一下答案,并且进行了实际的测试。
找到的方法:
1.math.js
该js的下载地址 https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.16.0/math.min.js
2.number-precision(建议使用么,本人使用) 内存小,可以和math.js和BigDecimal.js媲美
下载地址 https://github.com/nefe/number-precision/blob/master/build/index.js
使用方法

image.png

若网速有问题,可以复制一下代码,这里是js的写法

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
user ,medthor
NP.strip(num) // strip a number to nearest right number
NP.plus(num1, num2, num3, ...) // addition, num + num2 + num3, two numbers is required at least.
NP.minus(num1, num2, num3, ...) // subtraction, num1 - num2 - num3
NP.times(num1, num2, num3, ...) // multiplication, num1 * num2 * num3
NP.divide(num1, num2, num3, ...) // division, num1 / num2 / num3
NP.round(num, ratio) // round a number based on ratio
***************************************************************************** /
/
global Reflect, Promise */

function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}

/**

exports.strip = strip;
exports.plus = plus;
exports.minus = minus;
exports.times = times;
exports.divide = divide;
exports.round = round;
exports.digitLength = digitLength;
exports.float2Fixed = float2Fixed;
exports.enableBoundaryChecking = enableBoundaryChecking;
exports['default'] = index;

《==================这里是ts的写法=========================》
'use strict';

export interface NP {
strip: (num: number, precision?: number) => number;
plus: (...nums: number[]) => number;
minus: (...nums: number[]) => number;
times: (...nums: number[]) => number;
divide: (...nums: number[]) => number;
round: (num: number, ratio: number) => number;
digitLength: (num: number) => number;
float2Fixed: (num: number) => number;
enableBoundaryChecking: (flag?: boolean) => void;
}

/**

/**

/**

/**

/**

/**

/**

/**

/**

/**

let _boundaryCheckingState = true;

/**

const NP: NP = {
strip,
plus,
minus,
times,
divide,
round,
digitLength,
float2Fixed,
enableBoundaryChecking
};

export default NP;

上一篇下一篇

猜你喜欢

热点阅读