UtilsFormat.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { Label, tween, Tween, UITransform, Vec3, _decorator } from 'cc';
  2. const limit = [1000000000000, 1000000000, 1000000, 1000];
  3. const chars = ["T", "B", "M", "K"];
  4. export const SexKey = ["us_unspecified", "us_male", "us_female"];
  5. const Time_Desc = ["d","h","m","s"];
  6. export class UtilsFormat {
  7. public static toThousands(v: number) {
  8. let prefix = "";
  9. if (v < 0) {
  10. prefix = "-";
  11. v = -v;
  12. }
  13. let s = "" + v;
  14. let p = s.indexOf(".");
  15. let i = p >= 0 ? p : s.length;
  16. let str = "";
  17. while (i > 0) {
  18. if (!!str) {
  19. str = "," + str;
  20. }
  21. str = s.substring(i > 3 ? i - 3 : 0, i) + str;
  22. i -= 3;
  23. }
  24. if (p >= 0) {
  25. str = str + s.substring(p, s.length);
  26. }
  27. return !!prefix ? prefix + str : str;
  28. }
  29. //动态字符串 需要父节点要mask
  30. public static setMoveName(labelNode: Label, name: string) {
  31. labelNode.string = name;
  32. labelNode.updateRenderData(true);
  33. let nameMoveWidth = labelNode.getComponent(UITransform).width - labelNode.node.parent.getComponent(UITransform).width;
  34. Tween.stopAllByTarget(labelNode.node);
  35. labelNode.node.setPosition(0, 0);
  36. if (nameMoveWidth > 0) {
  37. let speed = labelNode.node.parent.getComponent(UITransform).width / 3;
  38. tween(labelNode.node)
  39. .delay(2)
  40. .to(3, { position: new Vec3(-labelNode.node.parent.getComponent(UITransform).width, 0, 0) })
  41. .delay(0.5)
  42. .call(() => { labelNode.node.toX(labelNode.getComponent(UITransform).width); })
  43. .to(labelNode.getComponent(UITransform).width / speed, { position: new Vec3(0, 0, 0) })
  44. .union()
  45. .repeatForever()
  46. .start()
  47. }
  48. }
  49. public static getShortName(name: string, num: number = 10, isReversed = false) {
  50. if (!isReversed) return name.length > num ? name.slice(0, num) + '...' : name;
  51. else return name.length > num ? '...' + name.slice(name.length - num, name.length) : name;
  52. }
  53. public static formatRate(rate: number) {
  54. return Math.floor(rate * 10000) / 100 + "%";
  55. }
  56. public static formatFlag(num:number){
  57. num = Math.floor(num / 1000) * 1000;
  58. let is_minus = num < 0;
  59. if (is_minus) {
  60. num = -num;
  61. }
  62. for (let i = 0; i < 4; i++) {
  63. if (num >= limit[i]) {
  64. num = (num * 1) / limit[i];
  65. let remain = num % 1;
  66. let str = "";
  67. if (remain > 0) {
  68. str = remain >= 0.01 ? num.toFixed(2) : Math.floor(num) + "";
  69. } else {
  70. str += num;
  71. }
  72. return is_minus ? "-" + str + chars[i] : str + chars[i];
  73. }
  74. }
  75. return "" + num;
  76. }
  77. public static formatMoney(num: number) {
  78. if(Math.abs(num) < 10000){
  79. return UtilsFormat.toThousands(num);
  80. }
  81. return UtilsFormat.formatFlag(num);
  82. }
  83. public static formatByLimit(num:number, limit:number){
  84. if(Math.abs(num) < limit)return UtilsFormat.toThousands(num);
  85. return UtilsFormat.formatFlag(num);
  86. }
  87. public static formatFlagLimit(num:number, limit:number){
  88. if(num < limit)return "" + num;
  89. return UtilsFormat.formatFlag(num);
  90. }
  91. public static PrefixInteger(num, n) {
  92. return (Array(n).join("0") + num).slice(-n);
  93. }
  94. public static schoolServerTime(s) {
  95. return s;
  96. }
  97. public static addZero(n) {
  98. if (n < 10) return "0" + n;
  99. return "" + n;
  100. }
  101. public static getDateStr(date: Date, format_str:string) {
  102. let str = "";
  103. let temp = "";
  104. for (let i = 0, len = format_str.length; i < len; i++) {
  105. temp = "";
  106. switch (format_str[i]) {
  107. case "n":
  108. temp = date.getFullYear() + "";
  109. temp = temp.slice(2, 4);
  110. break;
  111. case "N":
  112. temp = date.getFullYear() + "";
  113. break;
  114. case "Y":
  115. case "y":
  116. temp = UtilsFormat.addZero(date.getMonth() + 1);
  117. break;
  118. case "R":
  119. case "r":
  120. temp = UtilsFormat.addZero(date.getDate());
  121. break;
  122. case "S":
  123. case "s":
  124. temp = UtilsFormat.addZero(date.getHours());
  125. break;
  126. case "F":
  127. case "f":
  128. temp = UtilsFormat.addZero(date.getMinutes());
  129. break;
  130. case "M":
  131. case "m":
  132. temp = UtilsFormat.addZero(date.getSeconds());
  133. break;
  134. }
  135. if (temp != "") {
  136. str = str + temp;
  137. } else {
  138. str = str + format_str[i];
  139. }
  140. }
  141. return str;
  142. }
  143. /** !#zh isToday 判断是否为今天
  144. * @param {any} timestamp 时间戳
  145. */
  146. public static isToday(timestamp) {
  147. if (new Date(timestamp).toDateString() === new Date().toDateString()) {
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**几天前 */
  153. public static getDaysAgo(timestamp: Date) {
  154. let curStamp = new Date().getTime()
  155. const aDayStamp = 1000 * 60 * 60 * 24
  156. let offset = (curStamp - timestamp.getTime())
  157. return Math.ceil(offset / aDayStamp)
  158. }
  159. public static getCountdown(s: number, strs: Array<string>) {
  160. if (!strs) strs = ["d","h","m"];
  161. if (s > 86400) {
  162. let all_hour = Math.floor(s / 3600);
  163. let hour = all_hour % 24;
  164. let day = (all_hour - hour) / 24;
  165. return `${day}${strs[0]}:${hour}${strs[1]}`;
  166. } else {
  167. let all_minute = Math.floor(s / 60);
  168. let minute = all_minute % 60;
  169. let hour = (all_minute - minute) / 60;
  170. return `${hour}${strs[1]}:${minute}${strs[2]}`;
  171. }
  172. }
  173. public static getCountdown2(s:number, format_str:string, desc:Array<string>, limit:number){
  174. let str = "";
  175. let suffix = "";
  176. if(!desc)desc = Time_Desc;
  177. let count = 0;
  178. for (let i = 0, len = format_str.length; i < len; i++) {
  179. switch (format_str[i]) {
  180. case "R":
  181. case "r":
  182. count = Math.floor(s / 86400);
  183. if(count > 0){
  184. if(format_str[i] == "R")suffix += desc[0] + count;
  185. else str += (count + desc[0]);
  186. s -= count * 86400;
  187. limit--;
  188. }
  189. break;
  190. case "S":
  191. case "s":
  192. count = Math.floor(s / 3600);
  193. if(count > 0){
  194. str += (count + desc[1]);
  195. s -= count * 3600;
  196. limit--;
  197. }
  198. break;
  199. case "F":
  200. case "f":
  201. count = Math.floor(s / 60);
  202. str += (count + desc[2]);
  203. s -= count * 60;
  204. limit--;
  205. break;
  206. case "M":
  207. case "m":
  208. str += (s + desc[3]);
  209. limit--;
  210. break;
  211. }
  212. if(limit == 0)break;
  213. }
  214. return str + suffix;
  215. }
  216. public static getCountdownSFM(s:number){
  217. let second = s % 60;
  218. let minute = (s - second) / 60;
  219. let hour = Math.floor(minute / 60);
  220. minute = minute % 60;
  221. let func_add0 = UtilsFormat.addZero;
  222. return func_add0(hour) + ":" + func_add0(minute) + ":" + func_add0(second);
  223. }
  224. public static firstUpper(str: string) {
  225. return str[0].toUpperCase() + str.slice(1);
  226. }
  227. }