Utils.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * 用于判断浮点数是否相等
  3. * @param l
  4. * @param r
  5. */
  6. export function isEqualFloat(l: number, r: number) {
  7. const s = Math.abs(l - r);
  8. return s < 0.0000001;
  9. }
  10. export function todayDateStr() {
  11. const now = new Date()
  12. return `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
  13. }
  14. export function formaterYmdDate(date: Date) {
  15. return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
  16. }
  17. export function formatTime() {
  18. const now = new Date()
  19. return `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`
  20. }
  21. export function fixedFloat(l: number) {
  22. const value = Math.round(l * 10);
  23. return value / 10;
  24. }
  25. export function fixed2Float(l: number) {
  26. const value = Math.round(l * 100);
  27. return value / 100;
  28. }
  29. export function fixed3Float(l: number) {
  30. const value = Math.round(l * 1000);
  31. return value / 1000;
  32. }
  33. export function formatterCoin(coin: number) {
  34. if (coin >= 1000 && coin < 1000000) {
  35. return `${fixedFloat(coin / 1000)}K`
  36. }
  37. if (coin >= 1000000) {
  38. return `${fixedFloat(coin / 1000000)}M`
  39. }
  40. return `${coin}`
  41. }
  42. export function randomNumberString(length: number) {
  43. let str = '';
  44. for (let i = 0; i < length; i++) {
  45. str += Math.floor(Math.random() * 10);
  46. }
  47. return str;
  48. }
  49. export function paging<T>(page: number = 1, size: number = 10, list: T[]) {
  50. const start = (page - 1) * size;
  51. let end = start + size;
  52. if (list.length < start) {
  53. return [];
  54. }
  55. if (list.length < end) {
  56. end = list.length;
  57. }
  58. return list.slice(start, end);
  59. }
  60. export function nTCN(num: number) {
  61. const texts = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  62. const str = `${num}`
  63. let result = ``
  64. for (let index = 0; index < str.length; index++) {
  65. result += texts[Number(str.charAt(index))]
  66. }
  67. return result
  68. }
  69. export function shuffle<T>(arr: T[]) {
  70. let newArr = arr.concat();
  71. let j, temp;
  72. for (let i = arr.length; i > 0; i--) {
  73. j = Math.floor(Math.random() * i);
  74. temp = newArr[i - 1];
  75. newArr[i - 1] = newArr[j];
  76. newArr[j] = temp;
  77. }
  78. return newArr;
  79. }
  80. export function unique<T>(arr: T[]) {
  81. return Array.from(new Set(arr));
  82. }
  83. //验证字符串是否是数字
  84. export function isNumber(theObj: any) {
  85. return !isNaN(parseFloat(theObj)) && isFinite(theObj)
  86. }
  87. export function undefinedValue<T>(v: T, defaultValue: T) {
  88. return v == undefined || v == null ? defaultValue : v
  89. }
  90. export function formatToTwoDigits(number: number): string {
  91. if (number < 10) {
  92. return '0' + number;
  93. }
  94. return '' + number;
  95. }
  96. export function secondsToTimeFormat(seconds: number, needHour = false): string {
  97. if (seconds < 0) {
  98. return ``
  99. }
  100. const hours = Math.floor(seconds / 3600);
  101. const minutes = Math.floor((seconds % 3600) / 60);
  102. const remainingSeconds = Math.floor(seconds % 60);
  103. const formattedHours = formatToTwoDigits(hours);
  104. const formattedMinutes = formatToTwoDigits(minutes);
  105. const formattedSeconds = formatToTwoDigits(remainingSeconds);
  106. if (hours == 0 && !needHour) {
  107. return `${formattedMinutes}:${formattedSeconds}`;
  108. }
  109. return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
  110. }