// 金额转换使用整数分,避免万元与元之间的浮点乘法误差。 export function wanAmountValid(value: string): boolean { if (!/^[0-9]{1,5}([.][0-9]{1,6})?$/.test(value)) return false return wanToCents(value) <= 99999999999 } export function wanToCents(value: string): number { const parts = value.split('.') const fraction = parts.length > 1 ? parts[1] : '' return parseInt(parts[0]) * 1000000 + parseInt((fraction + '000000').substring(0, 6)) } export function wanToYuan(value: string): string { if (!wanAmountValid(value)) throw new Error('金额范围为 0–99999.999999 万元,最多六位小数') const cents = wanToCents(value) const fraction = (cents % 100).toString() return Math.floor(cents / 100).toString() + '.' + (fraction.length == 1 ? '0' + fraction : fraction) } export function yuanToWan(value: string): string { if (value.length == 0) return '' if (!/^[0-9]+([.][0-9]{1,2})?$/.test(value)) return value const parts = value.split('.') const fraction = parts.length > 1 ? parts[1] : '' const digits = parts[0] + (fraction + '00').substring(0, 2) const padded = '000000' + digits const whole = parseInt(padded.substring(0, padded.length - 6)).toString() const decimal = padded.substring(padded.length - 6).replace(/0+$/, '') return whole + (decimal.length > 0 ? '.' + decimal : '') }