UNPKG

@lzwme/asmd-calc

Version:

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

109 lines (108 loc) 2.44 kB
/** * 支持浮点数四则运算的链式操作类 */ export declare class AsmdCalc { private total; get value(): number; constructor(value: any); /** * 加法 * * ### 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 */ add(...args: any[]): 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 */ sub(...args: any[]): 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 */ mul(...args: any[]): 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 */ div(...args: any[]): 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 */ keepDotLength(len: number, isRounding?: boolean): 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 * ``` * */ toFixed(len: number): string; protected valueOf(): number; protected toString(): string; }