formatter.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { floatToFixed2 } from '@/utils'
  2. import Decimal from "decimal.js" // 具体文件中引入
  3. import { useSettingStore } from '@/store/modules/setting'
  4. const settingStore = useSettingStore()
  5. // 格式化金额【分转元】
  6. // @ts-ignore
  7. export const fenToYuanFormat = (_, __, cellValue: any, ___) => {
  8. return `¥${floatToFixed2(cellValue)}`
  9. }
  10. //四舍五入 保留两位小数
  11. export const fixedNum = (num) => {
  12. if (num != undefined) {
  13. num = parseFloat(num).toFixed(2)
  14. return num
  15. }
  16. }
  17. // 取小数点后两位
  18. export const formatNum = (val: string | number) => {
  19. const value = String(val)
  20. if (val && !isNaN(Number(val)) && (val !== undefined && val !== null)) {
  21. // 直接截取
  22. const decimalValue = new Decimal(value);
  23. return decimalValue.toFixed(9).slice(0, -7)
  24. }else if(val === 0){
  25. const decimalValue = new Decimal(val);
  26. return decimalValue.toFixed(2);
  27. }
  28. return ''
  29. }
  30. export const formatCeil = (val: string | number) => {
  31. if (val && !isNaN(Number(val)) && (val !== undefined && val !== null)) {
  32. return Math.ceil(val).toFixed(2)
  33. }else if(val === 0){
  34. return val
  35. }
  36. return ''
  37. }
  38. // 取整后保留两位小数
  39. export const formatRound = (val) => {
  40. if (!isNaN(Number(val))) {
  41. return Math.round(val).toFixed(2)
  42. }
  43. return ''
  44. }
  45. // 是否整数
  46. export const isInteger = (obj) => {
  47. if (!isNaN(Number(obj))) {
  48. return obj % 1 === 0;
  49. }
  50. return false
  51. }
  52. // 转中文
  53. // export const dealBigMoney = (n) => {
  54. // if (n == 0) return '零元'
  55. // n = n.toFixed(2)
  56. // if (!/^([-+])?(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
  57. // return '数据非法'
  58. // }
  59. // let unit = '千百拾亿千百拾万千百拾元角分'
  60. // let str = ''
  61. // n += '00'
  62. // const p = n > 0 ? n.indexOf('.') : n.indexOf('.') - 1
  63. // if (p >= 0) {
  64. // n = n.substring(0, p) + n.substr(p + 1, 2)
  65. // unit = unit.substr(unit.length - n.length)
  66. // }
  67. //
  68. // for (let i = 0; i < n.length; i++) {
  69. // str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i)
  70. // }
  71. // if (/^-/.test(n)) {
  72. // return (
  73. // '负' +
  74. // str
  75. // .replace(/零(千|百|拾|角)/g, '零')
  76. // .replace(/(零)+/g, '零')
  77. // .replace(/零(万|亿|元)/g, '$1')
  78. // .replace(/(亿)万|壹(拾)/g, '$1$2')
  79. // .replace(/^元零?|零分/g, '')
  80. // .replace(/元$/g, '元整')
  81. // )
  82. // } else {
  83. // return str
  84. // .replace(/零(千|百|拾|角)/g, '零')
  85. // .replace(/(零)+/g, '零')
  86. // .replace(/零(万|亿|元)/g, '$1')
  87. // .replace(/(亿)万|壹(拾)/g, '$1$2')
  88. // .replace(/^元零?|零分/g, '')
  89. // .replace(/元$/g, '元整')
  90. // }
  91. // }
  92. export const dealBigMoney = (n) => {
  93. if (n == 0) return '零元'
  94. // 提前处理负号
  95. const isNegative = n < 0
  96. n = Math.abs(n).toFixed(2)
  97. if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
  98. return '数据非法'
  99. }
  100. let unit = '千百拾亿千百拾万千百拾元角分'
  101. let str = ''
  102. n += '00'
  103. const p = n.indexOf('.')
  104. if (p >= 0) {
  105. n = n.substring(0, p) + n.substr(p + 1, 2)
  106. unit = unit.substr(unit.length - n.length)
  107. }
  108. for (let i = 0; i < n.length; i++) {
  109. str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i)
  110. }
  111. // 统一处理结果,不再区分正负
  112. const result = str
  113. .replace(/零(千|百|拾|角)/g, '零')
  114. .replace(/(零)+/g, '零')
  115. .replace(/零(万|亿|元)/g, '$1')
  116. .replace(/(亿)万|壹(拾)/g, '$1$2')
  117. .replace(/^元零?|零分/g, '')
  118. .replace(/元$/g, '元整')
  119. return isNegative ? '负' + result : result
  120. }
  121. // 计算转换
  122. export const formatDecimal = (val) => {
  123. if (!isNaN(Number(val))) {
  124. return val * 10000
  125. }
  126. return ''
  127. }
  128. export const formatRid = (val) => {
  129. if (!isNaN(Number(val))) {
  130. return val / 10000
  131. }
  132. return ''
  133. }
  134. export const extractInfoFromIdCard = (idCard)=> {
  135. if (!/^\d{17}[\dXx]$/.test(idCard)) {
  136. return null
  137. }
  138. const birthDate = idCard.substring(6, 14); // 提取出生日期部分,格式为 YYYY-MM-DD
  139. const birthDateStr = `${birthDate.substring(0,4)}-${birthDate.substring(4,6)}-${birthDate.substring(6,8)}`
  140. const genderBit = parseInt(idCard.substring(16, 17), 10); // 提取性别位,第17位
  141. const gender = genderBit % 2 === 0 ? 2 : 1; // 奇数表示男性,偶数表示女性
  142. return {
  143. birthDateStr: birthDateStr,
  144. gender: gender
  145. };
  146. }