| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- /**
- * 金额格式化工具
- * 后端统一使用整型(分)存储金额,前端在此转换展示
- */
- /**
- * 分 → 元,保留2位小数
- * @param {number|null} cents 金额(分)
- * @returns {string|null} "12.50" 或 null
- */
- export function formatPrice(cents) {
- if (cents == null || cents === undefined) return null
- return (cents / 100).toFixed(2)
- }
- /**
- * 分 → 元,带 ¥ 前缀
- * @param {number|null} cents 金额(分)
- * @returns {string} "¥12.50" 或 "登录查看"
- */
- export function formatPriceWithSymbol(cents) {
- if (cents == null || cents === undefined) return '登录查看'
- return '¥' + (cents / 100).toFixed(2)
- }
- /**
- * 千分比 → 百分比字符串
- * @param {number|null} permillage 千分比(235 = 23.5%)
- * @returns {string} "23.5%" 或 "--"
- */
- export function formatRate(permillage) {
- if (permillage == null || permillage === undefined) return '--'
- return (permillage / 10) + '%'
- }
- /**
- * 百分制评分 → 展示值
- * @param {number|null} value 百分制(450 = 4.5分)
- * @param {number} fixed 小数位数
- * @returns {string} "4.5" 或 "--"
- */
- export function formatScore(value, fixed) {
- if (fixed === undefined) fixed = 1
- if (value == null || value === undefined) return '--'
- return (value / 100).toFixed(fixed)
- }
|