//数字格式话方法,场景:
//(1)数字小于99999,每三位用逗号分隔;
//(2)数字大于99999,需要换算以万为单位末尾加“w”,并且四舍五入保留两位小数,如果整数位大于4位数,需要每三位用逗号分隔;
// 数字大于99999,加w
numFormatting (num) {
if (num > 99999) {
const numFormat = Math.floor(num / 10)
const rounded = (numFormat / 1000).toFixed(2)
const integer = rounded.split( . )[0]
const decimal = rounded.split( . )[1] === 00 ? rounded.split( . )[1] = : . + rounded.split( . )[1]
return this.formatNum(integer) + decimal + w
} else {
return this.formatNum(num)
// return (num || 0).toString().replace(/(d)(?=(?:d{3})+$)/g, $1, )
}
}
// 数字格式化,每3位用都逗号分隔
formatNum (num) {
let res =
let counter = 0
let splits = []
splits = num.toString().split( . )
num = (splits[0] || 0).toString()
for (let i = num.length - 1; i >= 0; i--) {
counter++
res = num.charAt(i) + res
if (!(counter % 3) && i !== 0) {
res = , + res
}
}
return splits.length > 1 ? res + . + splits[1] : res
},
// 60秒倒计时
getCode () {
if (!this.vCodeTimer) {
this.vCodeCount = this.countDown
this.vCodeShow = false
this.vCodeTimer = setInterval(() => {
if (this.vCodeCount > 0 && this.vCodeCount <= this.countDown) {
this.vCodeCount--
} else {
this.vCodeShow = true
clearInterval(this.vCodeTimer)
this.vCodeTimer = null
}
}, 1000)
}
},
// 校验手机号
isPhoneAvailable (phone) {
const myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
if (!myreg.test(phone)) {
return false
} else {
return true
}
},
// 正则 获取url里面的参数,params:key值,url:路径
getUrlParams (params, url) {
const reg = new RegExp( (^|&) + params + =([^&]*)(&|$) )
const r = url.substr(1).match(reg)
if (r == null || r.length < 1) {
return null
}
return r[2]
},
//多行文本省略号
/* splitContent
* text 需要处理的文字
* box 盒子容器
* maxRow 文字超出多少行显示省略号
* offset 偏移值
*/
splitContent(text, box, maxRow, offset) {
var re = /[^x00-xff]/g; // 匹配双字节字符
var style = getComputedStyle(box, null); // 获取盒子的样式
var w = parseInt(style.width);
var mSize = parseInt(style.fontSize);
var count = Math.floor(w / mSize); // 一行可显示多少字
var hasDouble = text.match(re);
var len = hasDouble ? (hasDouble.length + text.length) / 2 : text.length / 2;
var maxSize = count * maxRow; // 最多显示的文字个数
if (len > maxSize) {
for (var i = maxSize; i < text.length; i++) {
var mText = text.substr(0, i - 8);
var has = mText.match(re);
var mLen = has ? (has.length + mText.length) / 2 : mText.length / 2;
console.log(text.length,maxSize,mLen,offset)
if (mLen >= maxSize - offset) {
text = mText + ... + <a title=" + text + " href="javascript:;">更多</a> ;
break;
}
}
}
box.innerHTML = text;
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...