/** biome-ignore-all lint/style/useNamingConvention: wtf */ /** [a-calc文档](https://github.com/Autumn-one/a-calc-old/blob/main/README_ZH.md) a-calc是一个js计算库,解决js计算精度问题,然后还附带了一些常用的计算方法 使用场景: 1. 处理js计算精度问题 2. 提供常用的计算方法 @category ext */ import type { Fn, PositiveInteger, Result } from '@chzky/core'; import { IllegalExpressionError, NaNError } from '@chzky/core'; import * as a_calc from 'a-calc/es'; /** ## `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?: T; /** 返回值类型 */ type?: TY; }; /** ## `calc` : 对[a-calc](https://github.com/Autumn-one/a-calc-old/blob/main/README_ZH.md)的二次封装 用于数字的`精准计算`、`格式化`、`完备的舍入规则`、`单位计算` @example 重载 ```ts //normal type to call const r2 = calc('1+1000', {}, { fmt: [','] }).unwrap() assert.equal(r2, '1,001') //curry type to call const r3 = calc({ a: '1.123', b: '2.23' }, { unit: true })('a+b').unwrap() assert.equal(r3, '3.353') //catch NaNError calc({ a: '1.2.3' })('a+2') .match_ok((_ok)=>{assert.unreachable()}) .match_err((err) => { assert(err instanceof NaNError) }) ``` ### 错误处理: + 由于由于data_map导致的错误会返回`NaNError` + 由于表达式错误会返回`IllegalExpressionError` + 如果有重载错误会直接返回重载的错误,优先级高于`IllegalExpressionError` ### conf -> fmt: 格式化数组 #### 格式化列表规则: + `>|>=|<|<=|=`数字 表示限制小数位数,例: <=2 小数位数小于等于2 >3 小数位数必须大于3,这个等价于>=4 + `,` 输出为千分位数字字符串 + `/` 输出为分数 + `+` 输出的正数带+ 号 + `%` 输出百分比数字,可以和限制小数组合使用 + `!e` 输出为科学计数法 + `!n` 输出为数字而不是数字字符串,n可以大写,1.3.6版本之后这个优先级为最高,任何其他的格式化参数无法影响该参数。 + `!u` 从结果中去除单位 #### 四舍五入规则: + `~-` 去尾,默认的舍入规则 + `~+` 进一 + `~5` 四舍五入 + `~6` 四舍六入五成双 - 和`Number.toFixed()`算法相同 @category Function */ declare function internal_calc(expr: string | number, val_map?: FillData, conf?: CalcConf): Result; declare function internal_calc(val_map: FillData, conf?: CalcConf, other?: never): Fn>; export declare const calc: typeof internal_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=mod.d.ts.map