一些filter
2024-09-29 17:38:41
Vue.filter('money', (value, symbol = '', currency = '¥', decimals = 0) => {
const digitsRE = /(\d{3})(?=\d)/g
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : '$'
decimals = decimals != null ? decimals : 2
let stringified = Math.abs(value).toFixed(decimals)
let _int = decimals ?
stringified.slice(0, -1 - decimals) :
stringified
let i = _int.length % 3
let head = i > 0 ?
(_int.slice(0, i) + (_int.length > 3 ? symbol : '')) :
''
let _float = decimals ?
stringified.slice(-1 - decimals) :
''
let sign = value < 0 ? '-' : ''
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
_float
})
Vue.filter('percent', value => { // 百分比
return `${value}%`
})
Vue.filter('FormatMoney', (value, hasDecimal) => { // 千位数字以上*代替
value = parseFloat(value).toFixed(hasDecimal)
let result = '';
let b = value.split(".")
for (let i = 0; b[0].length > 3; i++) {
if (i == 0) {
result = ',' + b[0].slice(-3) + result
} else {
result = ',***' + result
}
b[0] = b[0].slice(0, b[0].length - 3)
}
if (b[0]) {
if (result.length > 0) {
for (var y = 0; b[0].length > y; y++) {
result = "*" + result
}
} else {
result = b[0] + result
}
}
if (b[1]) {
return result + '.' + b[1]
}
return result
})
Vue.filter('currency', (value, hasDecimal) => { // 数量格式化
if (hasDecimal) {
value = parseFloat(value).toFixed(hasDecimal)
}
return value && value.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') || 0
})
Vue.filter('time', (value, mark = '-') => {
if (!value)
return
let time = new Date(value)
return moment(time).format(`YYYY${mark}MM${mark}DD`)
})
最新文章
- 图解Javascript原型链
- 不可或缺 Windows Native (11) - C++: hello c++, C++ 与 C语言的区别小介
- Scalaz(36)- Free :实践-Free In Action - 实用体验
- 【bzoj1001】 BeiJing2006—狼抓兔子
- ssh安装与配置
- 你会在C#的类库中添加web service引用吗?
- selenium+python find_element_by_css_selector方法使用
- mysql innodb 引擎
- https_request请求接口返回数据
- Jsp 中文乱码,解决
- Entity Framework with MySQL 学习笔记一(复杂类型 Complex Types)
- java Socket使用注意
- 面试题25:最小的K个数
- iscroll实现移动端下拉刷新,上拉加载更多
- [Swift]LeetCode1 .两数之和 | Two Sum
- 29.Hibernate-二级缓存和session管理.md
- http协议返回码
- Centos 7 设置ssh只允许特定用户从指定的IP登录
- mysql 全文搜索(转载http://blog.csdn.net/manbujingxin/article/details/6656992)
- WCF 基础连接已经关闭: 服务器关闭了本应保持活动状态的连接。