| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- /**
- * utils/format.js —— 数字/价格/百分比格式化(详情页、实验室共用)。
- */
- export function money(v, digits) {
- const n = Number(v) || 0;
- if (digits != null) return '¥' + n.toFixed(digits);
- if (Math.abs(n) >= 1000) return '¥' + n.toLocaleString('zh-CN', { maximumFractionDigits: 0 });
- if (Number.isInteger(n)) return '¥' + n;
- return '¥' + n.toFixed(2).replace(/\.?0+$/, '');
- }
- /** 0.965 → '96.5%';入参若已是百分数(>1.5)则原样 */
- export function pct(v, digits = 1) {
- const n = Number(v) || 0;
- const p = n > 1.5 ? n : n * 100;
- return p.toFixed(digits).replace(/\.0$/, '') + '%';
- }
- export function num(v) {
- const n = Number(v) || 0;
- return n.toLocaleString('zh-CN');
- }
- export function esc(s) {
- return String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
- }
- /** 实验室滑块的合理范围(按键名 + 默认值推断) */
- const RANGE = {
- dTop: [10, 200], dBottom: [10, 200], height: [3, 60], wall: [0.4, 4.5], domeR: [0.3, 6],
- platform: [0, 4], recessR: [0.2, 4], recessDepth: [0.1, 8], screenD: [4, 180], chamferC: [0, 3],
- draft: [0, 5], split: [0, 1], cornerR: [0, 24], length: [10, 220], screenH: [4, 160], capR: [0.5, 12],
- bandD: [8, 40], ringD: [12, 140], ringBandW: [3, 20],
- };
- export function rangeFor(key, def) {
- const r = RANGE[key];
- if (r) return r;
- const d = Math.abs(Number(def) || 1);
- return [0, Math.max(1, Math.round(d * 4))];
- }
- export const STEP = { split: 0.01, draft: 0.1, wall: 0.1, domeR: 0.1, recessR: 0.1, chamferC: 0.1, capR: 0.1, platform: 0.1 };
- export function stepFor(key) { return STEP[key] ?? 0.1; }
- /** 形参键名的中文标签 */
- export const KEY_LABEL = {
- dTop: '顶部外径 / 宽', dBottom: '底部外径', height: '总高', wall: '壁厚', domeR: '顶部凸圆 R',
- platform: '顶部平台', recessR: '屏边凹圆 R', recessDepth: '沉台深度', screenD: '屏 / 沉台直径',
- chamferC: '底边倒角 C', draft: '拔模角', split: '拼色分割', cornerR: '水平角 R', length: '长度',
- screenH: '屏高', capR: '长边圆角 R', bandD: '表带连接宽', ringD: '内径', ringBandW: '截面宽',
- };
|