transform.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @description 将数字转换为中文简写数字
  3. * @param {Object} num
  4. */
  5. export function changeNumToHan(num) {
  6. var arr1 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  7. var arr2 = ['', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿']
  8. if (!num || isNaN(num)) return '零'
  9. var english = num.toString().split('')
  10. var result = ''
  11. for (var i = 0; i < english.length; i++) {
  12. var des_i = english.length - 1 - i // 倒序排列设值
  13. result = arr2[i] + result
  14. var arr1_index = english[des_i]
  15. result = arr1[arr1_index] + result
  16. }
  17. result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十') // 将【零千、零百】换成【零】 【十零】换成【十】
  18. result = result.replace(/零+/g, '零') // 合并中间多个零为一个零
  19. result = result.replace(/零亿/g, '亿').replace(/零万/g, '万') // 将【零亿】换成【亿】【零万】换成【万】
  20. result = result.replace(/亿万/g, '亿') // 将【亿万】换成【亿】
  21. result = result.replace(/零+$/, '') // 移除末尾的零
  22. // 将【一十】换成【十】
  23. result = result.replace(/^一十/g, '十')
  24. return result
  25. }
  26. /**
  27. * @description 将数字转换为小写字母
  28. * @param {Object} num
  29. */
  30. export function checkNum(num) {
  31. if (typeof(num) == 'number')
  32. return String.fromCharCode(0x60 + num);
  33. else
  34. return num;
  35. }
  36. // 对小于 10 的补零
  37. export function zeroFill(val) {
  38. return val < 10 ? `0${val}` : val;
  39. }
  40. /**
  41. * 将秒转为时:分:秒格式
  42. * @param {Number|String} val 秒
  43. * @param {'normal'|'chinese'} type 格式类型
  44. * @returns {String} hh:MM:ss 小于1小时返回 MM:ss
  45. */
  46. export function secondFormatConversion(val, type = 'normal') {
  47. const seconds = Math.floor(val); // 输入的秒数
  48. const hours = Math.floor(seconds / 3600); // 小时部分
  49. const minutes = Math.floor((seconds % 3600) / 60); // 分钟部分
  50. const remainingSeconds = seconds % 60; // 剩余的秒数
  51. // 使用零填充函数来格式化小时、分钟和秒
  52. const formattedHours = zeroFill(hours);
  53. const formattedMinutes = zeroFill(minutes);
  54. const formattedSeconds = zeroFill(remainingSeconds);
  55. // 根据时间范围返回不同的格式
  56. if (hours > 0) {
  57. if (type === 'chinese') {
  58. return `${hours}时${minutes}分${remainingSeconds}秒`;
  59. }
  60. return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
  61. }
  62. if (type === 'chinese') {
  63. return `${minutes}分${remainingSeconds}秒`;
  64. }
  65. return `${formattedMinutes}:${formattedSeconds}`;
  66. }
  67. /**
  68. * 将时间转为 时:分:秒 格式
  69. * @param {Date} date 时间戳
  70. * @returns {String} hh:MM:ss
  71. */
  72. export function timeFormatConversion(date) {
  73. return `${zeroFill(date.getHours())}:${zeroFill(date.getMinutes())}:${zeroFill(date.getSeconds())}`;
  74. }
  75. /**
  76. * 将时间转为 时:分:秒 格式
  77. * @param {String} 你好
  78. * @returns {Array} ['你','好']
  79. */
  80. export function convertStringToCollection(str) {
  81. if (!str) return [];
  82. return str.split('');
  83. }