chen.lin
昨天 b003a49794f49a329e2702918ecfc8d14b371d0d
rsf-admin/src/utils/common.js
@@ -1,13 +1,16 @@
/** 库存/数量显示:保留最多6位小数,去掉末尾多余的0(不强制补零) */
/** 前端数量显示:统一保留2位小数(仅展示,后端不变) */
export const formatQuantity = (value) => {
    if (value == null || value === '') return '0';
    if (value == null || value === '') return '0.00';
    const n = Number(value);
    if (Number.isNaN(n)) return '0';
    if (n < 0) return '0';
    return n % 1 === 0 ? String(n) : n.toFixed(6).replace(/\.?0+$/, '');
    if (Number.isNaN(n)) return '0.00';
    if (n < 0) return '0.00';
    return n.toFixed(2);
};
/** 用于 react-admin NumberField 的数量展示 options(2位小数) */
export const QTY_NUMBER_OPTIONS = { minimumFractionDigits: 2, maximumFractionDigits: 2 };
/** 校验最多 N 位小数,用于数量类字段;超过时返回错误信息并阻止提交 */
export const maxDecimalPlaces = (maxDecimals, message) => {
    const factor = Math.pow(10, maxDecimals);