initialize.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // import { numCapture } from "@/util/Filters.ts";
  2. /**
  3. * 价格数据截取
  4. * Num:截取值
  5. * type:截取类型 (Integer:整数部分,Decimals:小数部分)
  6. */
  7. export default {
  8. install(app, options) {
  9. console.log("Vue = ", app, options)
  10. // 添加全局方法或属性
  11. app.config.globalProperties.numCapture = (Num : Number | String, type : 'Integer' | 'Decimals') => {
  12. let N_V = '';
  13. const N = Num || '0.00'
  14. const N_A = String(N).split('.');
  15. switch (type) {
  16. case "Integer":
  17. N_V = N_A[0] || '0';
  18. break;
  19. case "Decimals":
  20. N_V = N_A[1] || '00';
  21. break;
  22. }
  23. return N_V
  24. };
  25. app.config.globalProperties.$addDecimals = function (num = 0, length = 8, type = true) {
  26. // length 小数的长度,type 是否需要处理
  27. try {
  28. if (!type) {
  29. return num
  30. }
  31. let num_str = num + "";
  32. if (num_str.indexOf('.') === -1) {
  33. num_str = `${num_str}.`
  34. for (let i = 1; i <= length; i++) {
  35. num_str += '0';
  36. }
  37. return num_str
  38. }
  39. const numArr = num_str.split('.');
  40. let integerNum = numArr[0],
  41. decimals = numArr[1];
  42. if (decimals.length === length) {
  43. return num
  44. }
  45. const addLength = length - decimals.length;
  46. for (let i = 1; i <= addLength && addLength > 0 ? addLength : 0; i++) {
  47. decimals += '0'
  48. }
  49. return `${integerNum}.${decimals}`
  50. } catch (err) {
  51. return num
  52. }
  53. };
  54. }
  55. }