| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- import { floatToFixed2 } from '@/utils'
- import Decimal from "decimal.js" // 具体文件中引入
- import { useSettingStore } from '@/store/modules/setting'
- const settingStore = useSettingStore()
- // 格式化金额【分转元】
- // @ts-ignore
- export const fenToYuanFormat = (_, __, cellValue: any, ___) => {
- return `¥${floatToFixed2(cellValue)}`
- }
- //四舍五入 保留两位小数
- export const fixedNum = (num) => {
- if (num != undefined) {
- num = parseFloat(num).toFixed(2)
- return num
- }
- }
- // 取小数点后两位
- export const formatNum = (val: string | number) => {
- const value = String(val)
- if (val && !isNaN(Number(val)) && (val !== undefined && val !== null)) {
- // 直接截取
- const decimalValue = new Decimal(value);
- return decimalValue.toFixed(9).slice(0, -7)
- }else if(val === 0){
- const decimalValue = new Decimal(val);
- return decimalValue.toFixed(2);
- }
- return ''
- }
- export const formatCeil = (val: string | number) => {
- if (val && !isNaN(Number(val)) && (val !== undefined && val !== null)) {
- return Math.ceil(val).toFixed(2)
- }else if(val === 0){
- return val
- }
- return ''
- }
- // 取整后保留两位小数
- export const formatRound = (val) => {
- if (!isNaN(Number(val))) {
- return Math.round(val).toFixed(2)
- }
- return ''
- }
- // 是否整数
- export const isInteger = (obj) => {
- if (!isNaN(Number(obj))) {
- return obj % 1 === 0;
- }
- return false
- }
- // 转中文
- // export const dealBigMoney = (n) => {
- // if (n == 0) return '零元'
- // n = n.toFixed(2)
- // if (!/^([-+])?(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
- // return '数据非法'
- // }
- // let unit = '千百拾亿千百拾万千百拾元角分'
- // let str = ''
- // n += '00'
- // const p = n > 0 ? n.indexOf('.') : n.indexOf('.') - 1
- // if (p >= 0) {
- // n = n.substring(0, p) + n.substr(p + 1, 2)
- // unit = unit.substr(unit.length - n.length)
- // }
- //
- // for (let i = 0; i < n.length; i++) {
- // str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i)
- // }
- // if (/^-/.test(n)) {
- // return (
- // '负' +
- // str
- // .replace(/零(千|百|拾|角)/g, '零')
- // .replace(/(零)+/g, '零')
- // .replace(/零(万|亿|元)/g, '$1')
- // .replace(/(亿)万|壹(拾)/g, '$1$2')
- // .replace(/^元零?|零分/g, '')
- // .replace(/元$/g, '元整')
- // )
- // } else {
- // return str
- // .replace(/零(千|百|拾|角)/g, '零')
- // .replace(/(零)+/g, '零')
- // .replace(/零(万|亿|元)/g, '$1')
- // .replace(/(亿)万|壹(拾)/g, '$1$2')
- // .replace(/^元零?|零分/g, '')
- // .replace(/元$/g, '元整')
- // }
- // }
- export const dealBigMoney = (n) => {
- if (n == 0) return '零元'
- // 提前处理负号
- const isNegative = n < 0
- n = Math.abs(n).toFixed(2)
- if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
- return '数据非法'
- }
- let unit = '千百拾亿千百拾万千百拾元角分'
- let str = ''
- n += '00'
- const p = n.indexOf('.')
- if (p >= 0) {
- n = n.substring(0, p) + n.substr(p + 1, 2)
- unit = unit.substr(unit.length - n.length)
- }
- for (let i = 0; i < n.length; i++) {
- str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i)
- }
- // 统一处理结果,不再区分正负
- const result = str
- .replace(/零(千|百|拾|角)/g, '零')
- .replace(/(零)+/g, '零')
- .replace(/零(万|亿|元)/g, '$1')
- .replace(/(亿)万|壹(拾)/g, '$1$2')
- .replace(/^元零?|零分/g, '')
- .replace(/元$/g, '元整')
- return isNegative ? '负' + result : result
- }
- // 计算转换
- export const formatDecimal = (val) => {
- if (!isNaN(Number(val))) {
- return val * 10000
- }
- return ''
- }
- export const formatRid = (val) => {
- if (!isNaN(Number(val))) {
- return val / 10000
- }
- return ''
- }
- export const extractInfoFromIdCard = (idCard)=> {
- if (!/^\d{17}[\dXx]$/.test(idCard)) {
- return null
- }
- const birthDate = idCard.substring(6, 14); // 提取出生日期部分,格式为 YYYY-MM-DD
- const birthDateStr = `${birthDate.substring(0,4)}-${birthDate.substring(4,6)}-${birthDate.substring(6,8)}`
- const genderBit = parseInt(idCard.substring(16, 17), 10); // 提取性别位,第17位
- const gender = genderBit % 2 === 0 ? 2 : 1; // 奇数表示男性,偶数表示女性
- return {
- birthDateStr: birthDateStr,
- gender: gender
- };
- }
|