utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export const getRankingStyle = (number) => {
  2. if (number <= 3) {
  3. return {
  4. 1: 'background: linear-gradient(180deg, #D40E0E 0%, #FF4D4D 100%);color: #fff',
  5. 2: 'background: linear-gradient(180deg, #FFBA2F 0%, #FFD47F 100%);color: #fff',
  6. 3: 'background: linear-gradient(180deg, #495FFF 0%, #9DA9FF 100%);color: #fff'
  7. }[number]
  8. }
  9. }
  10. export const getMapDataItem = (title, value = 0) => {
  11. return `
  12. <li style="width: 114px; height: 24px; background: #fff; margin-top: 8px; padding: 2px 8px; background: rgba(255, 255, 255, 0.9); border-radius: 2px; display: flex; justify-content: space-between" >
  13. <span style="color: #4E5969; font-size: 12px">${title}</span>
  14. <span style="color: #4E5969; font-size: 14px; font-weight: bold">${value}</span>
  15. </li>
  16. `
  17. }
  18. /**
  19. * 大数转换
  20. * @param {number} 要转换的数字
  21. */
  22. export const formatBigNumber = (number, decimalPlaces = 2) => {
  23. if (typeof number !== 'number' && typeof number !== 'string') {
  24. return console.warn(`Input must be a number or a string representation of a number. but get ${typeof number}`)
  25. }
  26. number = parseFloat(number)
  27. if (Math.abs(number) < 10000) {
  28. return { count: number.toFixed(decimalPlaces), unit: '' }
  29. }
  30. const units = ['', '万', '亿']
  31. const unitIndex = Math.floor((parseInt(number).toString().length - 1) / 4)
  32. const unit = units[Math.min(unitIndex, 2)]
  33. let result
  34. if (unitIndex > 0) {
  35. result = (number / Math.pow(10, unitIndex * 4)).toFixed(decimalPlaces)
  36. } else {
  37. result = number.toFixed(decimalPlaces)
  38. }
  39. return {
  40. count: result,
  41. unit
  42. }
  43. }