UNPKG

@lzwme/asmd-calc

Version:

支持浮点数精度的加减乘除四则运算 JS 库。

184 lines (183 loc) 5.11 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsmdCalc = void 0; // tslint:disable:typedef var calculation_1 = require("./calculation"); var utils_1 = require("./utils"); /** * 支持浮点数四则运算的链式操作类 */ var AsmdCalc = /** @class */ (function () { function AsmdCalc(value) { this.total = 0; if (!value) value = 0; var type = typeof value; if (value instanceof AsmdCalc) { this.total = value.total; } else if (type === 'number' || type === 'string') { this.total = Number(value); } else { throw Error('[AsmdCalcError] Invalid argument: ' + value); } return this; } Object.defineProperty(AsmdCalc.prototype, "value", { get: function () { return this.total; }, enumerable: false, configurable: true }); /** * 加法 * * ### Example (es module) * ```js * import { AsmdCalc } from 'asmd-calc'; * const a = new AsmdCalc(0.1); * console.log(+a.add(0.2, 3)); * // => 3.3 * console.log(+a.add(0.2).add(3)); * // => 6.5 * ``` * * @param args */ AsmdCalc.prototype.add = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.total = calculation_1.add.apply(void 0, __spreadArray([this.total], args, false)); return this; }; /** * 减法 * * ### Example (es module) * ```js * import { AsmdCalc } from 'asmd-calc'; * const a = new AsmdCalc(0.3); * console.log(+a.sub(0.1, 0.2)); * // => 0 * console.log(+a.sub(0.3).add(0.3)); * // => 0 * ``` * * @param args */ AsmdCalc.prototype.sub = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.total = calculation_1.sub.apply(void 0, __spreadArray([this.total], args, false)); return this; }; /** * 乘法 * * ### Example (es module) * ```js * import { AsmdCalc } from 'asmd-calc'; * const a = new AsmdCalc(0.1); * console.log(+a.mul(0.2)); * // => 0.02 * console.log(+a.mul(0.3).mul(30)); * // => 0.18 * ``` * * @param args */ AsmdCalc.prototype.mul = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.total = calculation_1.mul.apply(void 0, __spreadArray([this.total], args, false)); return this; }; /** * 除法 * * ### Example (es module) * ```js * import { AsmdCalc } from 'asmd-calc'; * const a = new AsmdCalc(0.3); * console.log(+a.div(0.1, 0.2).div(0.3)); * // => 50 * ``` * * @param args */ AsmdCalc.prototype.div = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.total = calculation_1.div.apply(void 0, __spreadArray([this.total], args, false)); return this; }; /** * 最多保留 N 位小数 * * ### Example (es module) * ```js * import { AsmdCalc } from 'asmd-calc'; * const a = new AsmdCalc(0.33366666); * console.log(+a.keepDotLength(6)); * // => 0.333666 * console.log(+a.keepDotLength(5, false)); * // => 0.33366 * console.log(+a.keepDotLength(4, true)); * // => 0.3337 * ``` * * @param len * @param isRounding */ AsmdCalc.prototype.keepDotLength = function (len, isRounding) { if (isRounding === void 0) { isRounding = false; } this.total = (0, calculation_1.keepDotLength)(this.total, len, isRounding); return this; }; /** * 最多保留 N 位小数 * * ### Example (es module) * ```js * import { AsmdCalc } from 'asmd-calc'; * const a = new AsmdCalc(1.45); * console.log(a.toFixed(1)); * // => 0.5 * console.log(a.toFixed(2)); * // => 1.45 * console.log(a.toFixed(3)); * // => 1.450 * ``` * */ AsmdCalc.prototype.toFixed = function (len) { return (0, calculation_1.toFixed)(this.total, len); }; AsmdCalc.prototype.valueOf = function () { return this.total; }; AsmdCalc.prototype.toString = function () { return (0, utils_1.toNonExponential)(this.total); }; return AsmdCalc; }()); exports.AsmdCalc = AsmdCalc;