一. 日期相关方法
二. 删除数组某个指定元素
deleteItem(arr, item) { // arr: 原数组; item: 要删除的元素
let _hasItem = arr.indexOf(item);
let _index;
if (_hasItem !== -1) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === item) {
_index = i;
}
}
arr.splice(_index, 1);
}
}
三. 千分符转化
// s: 需要处理的数字; n: 要保留的小数位数
money(s, n) {
let l, r, t = '';
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
l = s.split('.')[0].split('').reverse();
r = s.split('.')[1];
for (let i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '');
}
return t.split('').reverse().join('') + '.' + r;
}
四. 添加千分符并保留两位小数
function thousandsSeparator(v) {
let re = /\d{1,3}(?=(\d{3})+$)/g;
let newV;
if (v < 0) {
v = -v;
v = v.toFixed(2);
newV = v.toString().replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) {
return s1.replace(re, '$&,') + s2;
});
newV = '-' + newV;
} else {
v = v.toFixed(2);
newV = v.toString().replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) {
return s1.replace(re, '$&,') + s2;
});
}
return newV;
}
五. 经纬度转化为度分秒
// i=0-度°, i=1-分, i=2-秒
function formateLocation(v, i) {
if (v !== '') {
v = String(v);
let str1 = v.split('.');
let du = str1[0];
let tp = '0.' + str1[1];
tp = String(tp * 60);
let str2 = tp.split('.');
let fen = str2[0];
tp = '0.' + str2[1];
let miao = Math.round(tp * 60);
if (i == 0) {
return du;
} else if (i == 1) {
return fen;
} else if (i == 2) {
return miao;
}
} else {
return '';
}
}
实例
// 获取定位(经纬度)
getPos() {
this.getPosWord = '重新获取';
// console.log(this.locationInfo);
let lati = this.locationInfo.latitude || '';// 纬度
let longi = this.locationInfo.longitude || '';// 经度
this.result.northDegree = formateLocation(lati, 0);
this.result.northMinute = formateLocation(lati, 1);
this.result.northSecond = formateLocation(lati, 2);
this.result.eastDegree = formateLocation(longi, 0);
this.result.eastMinute = formateLocation(longi, 1);
this.result.eastSecond = formateLocation(longi, 2);
},
网友评论