首页 > 其他 > 详细

陶艾青

时间:2019-05-10 18:27:55      阅读:163      评论:0      收藏:0      [点我收藏+]

const roundMoney = (money) =>{ //数字四舍五入,保留两位小数
return (Math.round(money * 100) / 100)
}
roundMoney (17.855) //17.86

const formatMoney = (value, digit) => { // 金额格式化(数值=>金额)
if (!value && value !== 0) return ‘-‘
if (digit === 4) {
const formatNum = value.toFixed(digit)
return formatNum.slice(0, formatNum.length - 2).replace(/\B(?=(?:\d{3})+(?!\d))/g, ‘,‘) + formatNum.slice(formatNum.length - 2)
}
if (digit === 6) {
const formatNum = value.toFixed(digit)
return formatNum.slice(0, formatNum.length - 4).replace(/\B(?=(?:\d{3})+(?!\d))/g, ‘,‘) + formatNum.slice(formatNum.length - 4)
}
return value.toFixed(2).replace(/\B(?=(?:\d{3})+(?!\d))/g, ‘,‘)
}
formatMoney (1,4) //1.0000 1元
formatMoney (1,6) //1.000000 1元
formatMoney(1000000) //1,000,000.00 一百万

const formatNumber = (value) => { // 金额格式化(金额=>数值)
debugger;
let count = Number(value.replace(/,/g, ‘‘))
console.log(count,123)
return Number(value.replace(/,/g, ‘‘))
}

formatNumber (1.0) //1 1元
formatMoney (1.00) //1 1元
formatMoney (1,000,000.00) //1000000 一百万

Date.prototype.format = function (format) { // 日期格式化
const o = {
‘M+‘: this.getMonth() + 1,
‘d+‘: this.getDate(),
‘h+‘: this.getHours(),
‘H+‘: this.getHours(),
‘m+‘: this.getMinutes(),
‘s+‘: this.getSeconds(),
‘q+‘: Math.floor((this.getMonth() + 3) / 3),
S: this.getMilliseconds(),
}

if (/(y+)/.test(format)) { // y+ :匹配任何包含至少一个y的字符串
format = format.replace(RegExp.$1, ${this.getFullYear()}.substr(4 - RegExp.$1.length))
}
for (let k in o) {
if (new RegExp((${k})).test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : (00${o[k]}).substr(${o[k]}.length))
}
}
return format
}

// 日期格式化使用方法
let now = new Date(); // Mon Sep 10 2018 14:11:51 GMT+0800 (中国标准时间1)
let nowStr = now.format("yyyy-MM-dd hh:mm:ss");
console.log(‘nowStr‘,nowStr,now) // 2018-09-10 14:11:51

// 连字符转驼峰
String.prototype.hyphenToHump = function () {
return this.replace(/-(\w)/g, (...args) => {
return args[1].toUpperCase()
})
}

// 驼峰转连字符
String.prototype.humpToHyphen = function () {
return this.replace(/([A-Z])/g, ‘-$1‘).toLowerCase()
}

// 向数组中添加不存在的元素
monitorWarehouses.forEach((item, i) => item.rowNo = i + 1)

// 千位符格式化
方法一:
function toThousands(num) {
var result = ‘‘, counter = 0;
num = (num || 0).toString();
for (var i = num.length - 1; i >= 0; i--) {
counter++;
result = num.charAt(i) + result;
if (!(counter % 3) && i != 0) { result = ‘,‘ + result; }
}
return result;
}
toThousands(20000000000)

方法二:
function toThousands(num) {
var result = [ ], counter = 0;
num = (num || 0).toString().split(‘‘);
for (var i = num.length - 1; i >= 0; i--) {
counter++;
result.unshift(num[i]);
if (!(counter % 3) && i != 0) { result.unshift(‘,‘); }
}
return result.join(‘‘);
}
toThousands(20000000000000000)

// 去掉字符串的一部分
var fileBaseURL = ‘http://106.75.153.100:18080
fileBaseURL = fileBaseURL.replace("http://", ‘‘)
console.log(fileBaseURL) // 106.75.153.100:18080

// 返回的值可以判断是否禁止
checkboxT (row) {
if (row.status === 0 || row.status === 3 || row.status === 4) {
return 1
} else {
return 0
}
}

陶艾青

原文:https://www.cnblogs.com/IT123/p/10845651.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!