zoukankan      html  css  js  c++  java
  • 格式金钱【摘抄与网络,记录】

    // 格式化金钱
    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
    }

    效果:

     使用过滤器,使用如下:

    main.js导入:
    import { currency } from './common/currency.js' Vue.filter("currency", currency);
    //表格使用 <el-table-column prop="moneyGood" label="金额(元)" min-width="50" sortable align="center" > <template slot-scope="scope"> <span>{{scope.row.money| currency('¥')}}</span> </template> </el-table-column> //文本使用 <p class="cc">{{moneyGood| currency('¥')}}</p>

      

  • 相关阅读:
    js打开新窗口
    JS普通浏览器页面传参
    MUI之移动APP页面之间传递参数
    flex布局原理
    css3之Tab页面切换
    css之左边竖条的实现方法
    “程序猿”——要理清你的思路!
    win32调试——OutputDebugString
    des加密——补齐
    SVN 过滤文件
  • 原文地址:https://www.cnblogs.com/0520euv/p/12183164.html
Copyright © 2011-2022 走看看