js:1.获取几天后的日期、 2.遍历出两个日期之间的月份 、3.解决小数相加、相减的精度问题、4.数字金额转成大写金额、5.英文全角转换为半角

1. 获取几天后的日期

// 获取结束日期,startDate 开始日期(格式:2023-09-01),days相加的天数,XEUtils工具库
getEndDay(startDate, days) {
	let nDate = new Date(startDate).getTime()
	// 转化为毫秒
	let millSeconds = Math.abs(nDate) + days * 24 * 60 * 60 * 1000
	return XEUtils.toDateString(millSeconds)
},

2. 遍历出两个日期之间的月份

// 遍历出两个月份之间的月份集合(格式:2023-09, 2025-01)
export function getMonthIn(start, end) {
	const startYear = parseInt(start.split('-')[0])
	const startMonth = parseInt(start.split('-')[1])
	const endYear = parseInt(end.split('-')[0])
	const endMonth = parseInt(end.split('-')[1])

	let list = [] // 月份集合
	const yearNum = endYear - startYear // 年份差

	if (yearNum === 0) {
		let num = startMonth
		while (num <= endMonth) {
			list.push({
				// month: XEUtils.toDateString(startYear + '-' + num, 'yyyy年M月')
				month: startYear + '-' + num
			})
			num++
		}
	}
	if (yearNum >= 1) {
		// 前半部分月份
		let num = startMonth
		while (num <= 12) {
			list.push({
				// month: XEUtils.toDateString(startYear + '-' + num, 'yyyy年M月')
				month: startYear + '-' + num
			})
			num++
		}
		// 后半部分月份
		let num2 = endMonth
		while (num2 > 0) {
			list.push({
				// month: XEUtils.toDateString(endYear + '-' + num2, 'yyyy年M月')
				month: endYear + '-' + num2
			})
			num2--
		}
		// 中间年份的12个月份
		if (yearNum >= 2) {
			let i = startYear + 1 // 第二个年份开始
			while (i < endYear) {
				let j = 1 // 1月份
				while (j <= 12) {
					list.push({
						// month: XEUtils.toDateString(i + '-' + j, 'yyyy年M月')
						month: i + '-' + j
					})
					j++
				}
				i++
			}
		}
	}
	// 排序
	list.sort((a, b) => new Date(a.month).getTime() - new Date(b.month).getTime())

	return list
}

3. 解决小数相加、相减的精度问题

// 解决两个小数相加的精度
export function accountAdd(arg1, arg2) {
	let r1, r2, m
	try {
		r1 = arg1.toString().split('.')[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split('.')[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2))
	return Math.round(arg1 * m + arg2 * m) / m
}

// 解决两个小数相减的精度
export const accountSub = (arg1, arg2) => {
	let r1, r2, m, n
	try {
		r1 = arg1.toString().split('.')[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split('.')[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2)) // last modify by deeka //动态控制精度长度
	n = r1 >= r2 ? r1 : r2
	return ((arg1 * m - arg2 * m) / m).toFixed(n)
}

4. 金额转成大写

// 金额转成大写
export function moneyToCaptital(num) {
	let minus = ''
	let m_str = ''
	const text = num.toString()
	const zh = '零壹贰叁肆伍陆柒捌玖'
	if (text.indexOf('-') === 0) {
		num = text.replace('-', '')
		minus = '负'
	}
	let money_num = Number(num)
	let money = Math.round(money_num * 100).toString(10)
	const len = money.length
	for (let i = 0; i < len; i++) {
		m_str = m_str + zh.charAt(parseInt(money.charAt(i))) + sitToUnit(len - i - 1)
	}
	m_str = m_str.replace('零分', '')
	m_str = m_str.replace('零角', '零')
	let yy = 0
	while (true) {
		let len = m_str.length
		m_str = m_str.replace('零元', '元')
		m_str = m_str.replace('零万', '万')
		m_str = m_str.replace('零亿', '亿')
		m_str = m_str.replace('零仟', '零')
		m_str = m_str.replace('零佰', '零')
		m_str = m_str.replace('零零', '零')
		m_str = m_str.replace('零拾', '零')
		m_str = m_str.replace('亿万', '亿零')
		m_str = m_str.replace('万仟', '万零')
		m_str = m_str.replace('仟佰', '仟零')
		yy = m_str.length
		if (yy === len) {
			break
		}
	}
	yy = m_str.length
	if (m_str.charAt(yy - 1) === '零') {
		m_str = m_str.substring(0, yy - 1)
	}
	yy = m_str.length
	if (m_str.charAt(yy - 1) === '元') {
		// m_str = m_str + '整'
		m_str = m_str
	}
	return minus + m_str
}

export function sitToUnit(a) {
	if (a > 10) {
		a = a - 8
		return sitToUnit(a)
	}
	const zh = '分角元拾佰仟万拾佰仟亿'
	return zh.charAt(a)
}

5. 英文全角转换为半角

// 英文全角转换为半角
export function ToCDB(str) {
	let result = ''
	for (var i = 0; i < str.length; i++) {
		let code = str.charCodeAt(i)
		if (code >= 65281 && code <= 65374) {
			result += String.fromCharCode(str.charCodeAt(i) - 65248)
		} else if (code == 12288) {
			result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32)
		} else {
			result += str.charAt(i)
		}
	}
	return result
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值