js format 时间金钱格式化方法

收录了前端开发时间金额等常用format方法

1.金额 3456 改为$3,456.00

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
}

js format 时间金钱格式化方法

原文地址:http://www.cnblogs.com/Abner5/p/7256060.html


最新回复(0)
/jishu8Jn75xAvl1ZoiY7X8DfB_2FNLa_2BTmc0Ghkh_2B1c4Q_3D_3D4719398
8 简首页