Vue.js货币格式化函数
2024-10-14 01:29:19
函数:
const digitsRE = /(\d{3})(?=\d)/g export function currency (value, currency, decimals) {
value = parseFloat(value)
if (!isFinite(value) || (!value && value !== 0)) return ''
currency = currency != null ? currency : '$'
decimals = decimals != null ? decimals : 2
var stringified = Math.abs(value).toFixed(decimals)
var _int = decimals
? stringified.slice(0, -1 - decimals)
: stringified
var i = _int.length % 3
var head = i > 0
? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
: ''
var _float = decimals
? stringified.slice(-1 - decimals)
: ''
var sign = value < 0 ? '-' : ''
return sign + currency + head +
_int.slice(i).replace(digitsRE, '$1,') +
_float
}
引入:
import {currency} from './../util/currency'
定义局部过滤器:
filters:{
currency:currency
},
使用:
<div class="item-total">
Item total: <span class="total-price">{{totalPrice|currency('$')}}</span>
</div>
全局过滤器:在main.js
import {currency} from './util/currency'
Vue.filter("currency",currency);
使用:
<div class="cart-tab-4">
<div class="item-price-total">{{(item.productNum*item.salePrice)|currency('$')}}</div>
</div>
效果:
最新文章
- 构建Maven聚合工程
- 3D打印公司网站dedecms大气模板
- 微信小程序开发初体验--教你开发小程序
- javascript函数的定义与执行
- 【poj2828】Buy Tickets
- XML的介绍
- 手把手教你在Windows下使用MinGW编译libav(参考libx264的编入)
- 【军哥谈CI框架】之入门教程之第二讲:分析CI结构和CI是怎么工作的
- UVA 1646 Edge Case
- Hibernate查询之Criteria查询
- WinSock IO模型 -- WSAEventSelect模型事件触发条件说明
- bzoj2346[Baltic 2011]Lamp
- IIS 部署WCF服务注意事项
- 第七次作业——numpy统计分布显示
- JIRA日期格式设置
- experiment 3
- activiti自己定义流程之整合(四):整合自己定义表单部署流程定义
- Unix域套接字简介
- 新浪微博API的使用Python
- 原生Ajax使用