add ProductStockPackageSummary.vue
This commit is contained in:
@@ -100,6 +100,29 @@ export function convertBaseQuantityToPackageQuantity(baseQuantity: number | stri
|
||||
return formatDecimal(rounded, 3, 3);
|
||||
}
|
||||
|
||||
/** 将非负基础数量精确拆分为整包装和基础单位余数,不对余数取整。 */
|
||||
export function splitBaseQuantityIntoPackages(baseQuantity: number | string, baseQuantityPerUnit: number | string): { whole: string; remainder: string } | undefined {
|
||||
const quantityParts = toDecimalParts(baseQuantity, 3);
|
||||
const unitParts = toDecimalParts(baseQuantityPerUnit, 6);
|
||||
if (!quantityParts || !unitParts) {
|
||||
return undefined;
|
||||
}
|
||||
const scale = Math.max(quantityParts.scale, unitParts.scale);
|
||||
const factor = 10n ** BigInt(scale);
|
||||
const quantity = quantityParts.value * 10n ** BigInt(scale - quantityParts.scale);
|
||||
const unit = unitParts.value * 10n ** BigInt(scale - unitParts.scale);
|
||||
if (unit <= factor) {
|
||||
return undefined;
|
||||
}
|
||||
const remainder = quantity % unit;
|
||||
const integer = (remainder / factor).toString();
|
||||
const fraction = (remainder % factor).toString().padStart(scale, "0").replace(/0+$/, "");
|
||||
return {
|
||||
whole: (quantity / unit).toString(),
|
||||
remainder: fraction ? `${integer}.${fraction}` : integer
|
||||
};
|
||||
}
|
||||
|
||||
/** 精确相加两个最多三位小数的数量。 */
|
||||
export function addPackageQuantity(left: number | string, right: number | string): string | undefined {
|
||||
const leftParts = toQuantityParts(left);
|
||||
|
||||
Reference in New Issue
Block a user