码迷,mamicode.com
首页 > 其他好文 > 详细

vue中常用插件(货币、日期)

时间:2018-11-10 12:41:30      阅读:1740      评论:0      收藏:0      [点我收藏+]

标签:import   turn   price   git   ace   小数   符号   seconds   vuejs   

货币插件:
价格格式化

// https://github.com/vuejs/vuex/blob/dev/examples/shopping-cart/currency.js

const digitsRE = /(\d{3})(?=\d)/g
/**
 * [currency 金额格式化函数]
 * @param  {[type]} value    [传进来的值]
 * @param  {[type]} currency [货币符号]
 * @param  {[type]} decimals [小数位数]
 * @return {[type]}          [description]
 */
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‘ Vue.filter("currency",currency)
引用(局部):import {currency} from ‘./util/currency‘
// filters:{ // 定义局部过滤器
// currency:currency // currency.js传过来的本就是函数
// },
用法(局部): {{totalPrice | currency(‘$‘)}}
用法(全局): {{totalPrice | currency()}}

日期插件:

/**
 * Created by jacksoft on 17/4/26.
 */
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
module.exports = {};

vue中常用插件(货币、日期)

标签:import   turn   price   git   ace   小数   符号   seconds   vuejs   

原文地址:https://www.cnblogs.com/vientiane/p/9938615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!