index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import accounting from "accounting";
  2. const setIntervalImmediately = (fn, duration) =>
  3. setInterval((() => (fn(), fn))(), duration);
  4. function formatImgSrc(srcArr) {
  5. if (Array.isArray(srcArr) && srcArr.length > 0) {
  6. return srcArr[0] ?? "";
  7. }
  8. return "";
  9. }
  10. const isEmptyValue = (value) => {
  11. if (value == null) return true
  12. if (Array.isArray(value) && value.length === 0) return true
  13. if (typeof value === 'object' && Object.keys(value).length === 0) return true
  14. return false
  15. }
  16. const formatNumber = (n) => {
  17. let num = Number(n ?? '')
  18. // 如果数字大于或等于10,000万,则显示为9999w+
  19. if (num >= 10000 * 10000) {
  20. return '9999万+'
  21. }
  22. // 如果数字大于或等于1万,则转换为以“万”为单位,不四舍五入,最多保留两位小数
  23. else if (num >= 10000) {
  24. let w = Math.floor(num / 10000) // 取整,避免四舍五入
  25. let remainder = num % 10000
  26. // 计算小数部分并限制为最多两位
  27. let decimalPart = ''
  28. if (remainder > 0) {
  29. // 将余数转换为两位小数,但不进行四舍五入
  30. decimalPart = ('.' + Math.floor(remainder / 100)).slice(0, 2)
  31. // 移除结尾的0,如果有的话
  32. decimalPart = decimalPart.replace(/\.?0+$/, '')
  33. }
  34. return `${w}${decimalPart}万`
  35. }
  36. // 对于小于1万的数字直接输出
  37. else {
  38. return accounting.formatNumber(num)
  39. }
  40. }
  41. export { setIntervalImmediately, formatImgSrc, formatNumber, isEmptyValue };