48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
export function formatYuan(v) {
|
||
if (v == null || v === '') return '0.00'
|
||
const n = Number(v)
|
||
if (Number.isNaN(n)) return '0.00'
|
||
return n.toFixed(2)
|
||
}
|
||
|
||
export function formatFenAsYuan(fen) {
|
||
if (fen == null || Number.isNaN(fen)) return '0.00'
|
||
return (fen / 100).toFixed(2)
|
||
}
|
||
|
||
/**
|
||
* 计量 + 单位 +(规格),如 500 克(礼盒装);无规格时不显示括号
|
||
*/
|
||
export function formatMeasureUnitSpec(measureValue, unitName, spec) {
|
||
const unitText = (unitName || '').trim()
|
||
const specText = spec != null ? String(spec).trim() : ''
|
||
|
||
let base = ''
|
||
if (measureValue != null && measureValue !== '') {
|
||
const num = Number(measureValue)
|
||
if (!Number.isNaN(num)) {
|
||
const measureText = String(num).replace(/\.?0+$/, '')
|
||
base = unitText ? `${measureText} ${unitText}` : measureText
|
||
} else {
|
||
const measureText = String(measureValue).trim()
|
||
base = unitText ? `${measureText} ${unitText}` : measureText
|
||
}
|
||
} else if (unitText) {
|
||
base = unitText
|
||
} else if (!specText) {
|
||
return '-'
|
||
}
|
||
|
||
if (specText) {
|
||
return base ? `${base}(${specText})` : `(${specText})`
|
||
}
|
||
return base || '-'
|
||
}
|
||
|
||
/** 数量 × 计量单位(规格),如 10×500 克(礼盒装) */
|
||
export function formatQtyMeasureUnitSpec(qty, measureValue, unitName, spec) {
|
||
const inner = formatMeasureUnitSpec(measureValue, unitName, spec)
|
||
if (inner === '-') return String(qty)
|
||
return `${qty}×${inner}`
|
||
}
|