format.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }