format.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Integer cents → display format utilities
  3. * All backend prices now stored as Integer cents (1元 = 100)
  4. * All backend rates stored as Integer bps (100% = 10000)
  5. * All backend scores stored as Integer percentile (5.00 = 500)
  6. */
  7. /**
  8. * Format cents to yuan string (no symbol)
  9. * 9900 → '99.00'
  10. */
  11. export function formatPrice (cents) {
  12. if (cents === null || cents === undefined) return '0.00'
  13. return (Number(cents) / 100).toFixed(2)
  14. }
  15. /**
  16. * Format cents to yuan with ¥ symbol
  17. * 9900 → '¥99.00'
  18. */
  19. export function formatPriceWithSymbol (cents) {
  20. return '¥' + formatPrice(cents)
  21. }
  22. /**
  23. * Format bps to percentage string
  24. * 235 → '2.35%' 1000 → '10.00%'
  25. */
  26. export function formatRate (bps) {
  27. if (bps === null || bps === undefined) return '0.00%'
  28. return (Number(bps) / 100).toFixed(2) + '%'
  29. }
  30. /**
  31. * Format percentile score to decimal display
  32. * 500 → '5.00' 435 → '4.35'
  33. */
  34. export function formatScore (percentile) {
  35. if (percentile === null || percentile === undefined) return '0.00'
  36. return (Number(percentile) / 100).toFixed(2)
  37. }
  38. /**
  39. * Format ISO datetime string to Chinese style: 'yyyy-MM-dd HH:mm:ss' -> 'yyyy年MM月dd日 HH:mm:ss'
  40. * Handles: null/undefined -> '-', already-formatted strings -> idempotent passthrough
  41. */
  42. export function formatDateTime (t) {
  43. if (t === null || t === undefined || t === '') return '-'
  44. const s = String(t).replace('T', ' ').trim()
  45. const m = s.match(/^(\d{4})-(\d{2})-(\d{2})(.*)$/)
  46. if (!m) return s
  47. const timePart = m[4] ? ' ' + m[4].trim().substring(0, 8) : ''
  48. return m[1] + '年' + m[2] + '月' + m[3] + '日' + timePart
  49. }
  50. /**
  51. * Format ISO datetime string to Chinese date only: 'yyyy-MM-dd HH:mm:ss' -> 'yyyy年MM月dd日'
  52. */
  53. export function formatDate (t) {
  54. if (t === null || t === undefined || t === '') return '-'
  55. const dt = formatDateTime(t)
  56. if (dt === '-') return '-'
  57. return dt.indexOf(' ') >= 0 ? dt.substring(0, dt.indexOf(' ')) : dt
  58. }