add AIAgent
This commit is contained in:
@@ -85,6 +85,21 @@ export function convertPackageQuantity(quantity: number | string, fromBaseQuanti
|
||||
return formatDecimal(numerator / denominator, 3, 3);
|
||||
}
|
||||
|
||||
/** 将基础单位数量换算为业务单位展示数量,结果最多保留三位小数。 */
|
||||
export function convertBaseQuantityToPackageQuantity(baseQuantity: number | string, baseQuantityPerUnit: number | string): string | undefined {
|
||||
const quantityParts = toDecimalParts(baseQuantity, 3);
|
||||
const unitParts = toDecimalParts(baseQuantityPerUnit, 6);
|
||||
if (!quantityParts || !unitParts || unitParts.value === 0n) {
|
||||
return undefined;
|
||||
}
|
||||
const numerator = quantityParts.value * 10n ** BigInt(unitParts.scale + 3);
|
||||
const denominator = 10n ** BigInt(quantityParts.scale) * unitParts.value;
|
||||
const quotient = numerator / denominator;
|
||||
const remainder = numerator % denominator;
|
||||
const rounded = denominator < remainder * 2n ? quotient + 1n : quotient;
|
||||
return formatDecimal(rounded, 3, 3);
|
||||
}
|
||||
|
||||
/** 精确相加两个最多三位小数的数量。 */
|
||||
export function addPackageQuantity(left: number | string, right: number | string): string | undefined {
|
||||
const leftParts = toQuantityParts(left);
|
||||
|
||||
Reference in New Issue
Block a user