123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- // 数字转汉字方法
- export const numToZh = num => {
- if (!num && num !== 0) return ''
- const chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
- const chnUnitSection = ['', '万', '亿', '万亿', '亿亿']
- const chnUnitChar = ['', '十', '百', '千']
- const numToChn = num => {
- const index = num.toString().indexOf('.')
- if (index !== -1) {
- const str = num.toString().slice(index)
- let a = '点'
- for (let i = 1; i < str.length; i++) {
- a += chnNumChar[parseInt(str[i])]
- }
- return a
- } else {
- return ''
- }
- }
- // 定义在每个小节的内部进行转化的方法,其他部分则与小节内部转化方法相同
- const sectionToChinese = section => {
- let str = ''
- let chnstr = ''
- let zero = false
- let count = 0 // zero为是否进行补零, 第一次进行取余由于为个位数,默认不补零
- while (section > 0) {
- var v = section % 10 // 对数字取余10,得到的数即为个位数
- if (v === 0) {
- // 如果数字为零,则对字符串进行补零
- if (zero) {
- zero = false // 如果遇到连续多次取余都是0,那么只需补一个零即可
- chnstr = chnNumChar[v] + chnstr
- }
- } else {
- zero = true // 第一次取余之后,如果再次取余为零,则需要补零
- str = chnNumChar[v]
- str += chnUnitChar[count]
- chnstr = str + chnstr
- }
- count++
- section = Math.floor(section / 10)
- }
- return chnstr
- }
- const a = numToChn(num)
- num = Math.floor(num)
- let unitPos = 0
- let strIns = ''
- let chnStr = ''
- let needZero = false
- if (num === 0) {
- return chnNumChar[0]
- }
- while (num > 0) {
- const section = num % 10000
- if (needZero) {
- chnStr = chnNumChar[0] + chnStr
- }
- strIns = sectionToChinese(section)
- strIns += section !== 0 ? chnUnitSection[unitPos] : chnUnitSection[0]
- chnStr = strIns + chnStr
- needZero = section < 1000 && section > 0
- num = Math.floor(num / 10000)
- unitPos++
- }
- let result = chnStr + a
- const temp = result.split('')
- if (temp[0] === '一' && temp[1] === '十') {
- temp.splice(0, 1)
- result = temp.join('')
- }
- return result
- }
- // 活动类型
- export const seasonType = type => {
- return type ? '密训营' : '实战营'
- }
- // 时间戳转换
- export function dateFormatter(timestamp, fmt) {
- fmt = fmt || "yyyy-MM-dd";
- if (typeof timestamp === "string") {
- timestamp = timestamp.replace(/-/g, '/')
- }
- const $this = new Date(timestamp);
- const o = {
- "M+": $this.getMonth() + 1,
- "d+": $this.getDate(),
- "h+": $this.getHours(),
- "m+": $this.getMinutes(),
- "s+": $this.getSeconds(),
- "q+": Math.floor(($this.getMonth() + 3) / 3),
- S: $this.getMilliseconds()
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- ($this.getFullYear() + "").substr(4 - RegExp.$1.length)
- );
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1 ?
- o[k] :
- ("00" + o[k]).substr(("" + o[k]).length)
- );
- }
- }
- return fmt;
- }
- // 倒计时
- export const countDown = (s1, s2) => {
- const _c = n => n < 10 ? `0${n}` : n
- let max = Math.max(s1, s2)
- let min = Math.min(s1, s2)
- const _x = Math.floor((max - min) / 1000)
- let d = Math.floor(_x / 86400)
- let h = Math.floor((_x % 86400) / 3600)
- let m = Math.floor((_x - 86400 * d - h * 3600) / 60)
- let s = Math.floor(_x - 86400 * d - h * 3600 - m * 60)
- return `${_c(d)}天${_c(h)}时${_c(m)}分${_c(s)}秒`
- }
- export const getName = value => {
- return name.length > 10 ? name.slice(0, 10) + '...' : name;
- }
- // 活动类型
- export const activityType = t => {
- t = Number(t)
- return t ? '密训营' : '实战营'
- }
|