import type { Fn, PositiveInteger, Result } from '../../mod.js'; import { IllegalExpressionError, NaNError } from '../../mod.js'; import { a_calc } from '../Ext/aCalc.js'; /** ## `CalcFmt` : calc格式化选项, */ export declare const CalcFmt: { /** 千分位数字字符串 */ readonly Thousandle: ","; /** A分之B中的'/' */ readonly DividedBy: "/"; /** 正数带+号 */ readonly Positive: "+"; /** 对结果进行百分比格式化(如果是普通数字会乘以100) */ readonly Percent: "%"; /** 科学计数法 */ readonly Exponential: "!e"; /** 去除单位 */ readonly NoUnit: "!u"; /** @default 去尾四舍五入 */ readonly Floor: "~-"; /** 进一四舍五入 */ readonly Celi: "~+"; /** 四舍五入 */ readonly Round: "~5"; /** 四舍六入五成双 - 和`Number.toFixed()`算法相同 */ readonly Rounding: "~6"; /** 最多保留`N`位小数(只做截取) */ readonly ToFixed: (limit: PositiveInteger) => Fmt; /** 保留`N`位小数,如果不足就补0(只做截取) */ readonly MustToFixed: (limit: PositiveInteger) => Fmt; }; type DecimalLimit = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'; type OtherFM = ',' | '/' | '+' | '%' | '!e' | '!u' | '~-' | '~+' | '~5' | '~6'; type Fmt = `>${DecimalLimit}` | `<${DecimalLimit}` | `=${DecimalLimit}` | `>=${DecimalLimit}` | `<=${DecimalLimit}` | OtherFM; type FillData = Record | Array>; type CalcConf = { /** 结果格式化数组 */ fmt?: Array; /** 是否开启结果缓存,开启之后遇到后一个相同表达式会直接给出结果 */ memo?: boolean; /** 是否带上单位进行计算 */ unit?: boolean; /** 错误替代值,结算发生错误时用于替代的错误值 */ error?: Result; }; /** ## calc : 对[calc](https://github.com/Autumn-one/a-calc-old/blob/main/README_ZH.md)的二次封装 用于 js数字精准计算、数字格式化、完备的舍入规则、单位计算 @example 重载 ```ts //normal type const r2 = calc('1+1000', {}, { fmt: [','] }).unwrap() assertEquals(r2, '1,001') //curry type const r3 = calc({ a: '1.123', b: '2.23' }, { unit: true })('a+b').unwrap() assertEquals(r3, '3.353') //catch NaNError calc({ a: '1.2.3' })('a+2').match_err((err) => { assert(err instanceof NaNError) }) ``` @tips conf -> fmt: 格式化数组 ### 格式化列表规则: + `>|>=|<|<=|=`数字 表示限制小数位数,例: <=2 小数位数小于等于2 >3 小数位数必须大于3,这个等价于>=4 + `,` 输出为千分位数字字符串 + `/` 输出为分数 + `+` 输出的正数带+ 号 + `%` 输出百分比数字,可以和限制小数组合使用 + `!e` 输出为科学计数法 + `!n` 输出为数字而不是数字字符串,n可以大写,1.3.6版本之后这个优先级为最高,任何其他的格式化参数无法影响该参数。 + `!u` 从结果中去除单位 ### 四舍五入规则: + `~-` 去尾,默认的舍入规则 + `~+` 进一 + `~5` 四舍五入 + `~6` 四舍六入五成双 - 和`Number.toFixed()`算法相同 @category Function */ declare function _calc(expr: string | number, val_map?: FillData, conf?: CalcConf): Result; declare function _calc(val_map: FillData, conf?: CalcConf, other?: never): Fn>; export declare const calc: typeof _calc; export declare const calc_plus: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_sub: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_mul: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_div: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_idiv: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_pow: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_mod: (a: number | string, b: number | string, type: T) => T extends "number" ? number : string; export declare const calc_parse_thousands: (str: string) => string; export declare const calc_origin: a_calc.CalcWrap; export {}; //# sourceMappingURL=calc.d.ts.map