表格,表单等数字转换为千分位显示

封装方法:

function clearNum(obj) {
    obj.value = (numberFormat(obj.value,2));
}

function numberFormat (number, decimals, decPoint, thousandsSep, roundtag) {
    number = (number + '').replace(/[^0-9+-Ee.]/g, '')
    roundtag = roundtag || 'ceil' // 'ceil','floor','round'
    var n = !isFinite(+number) ? 0 : +number
    var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
    var sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
    var dec = (typeof decPoint === 'undefined') ? '.' : decPoint
    var s = ''
    var toFixedFix = function (n, prec) {
        var k = Math.pow(10, prec)
        return '' + parseFloat(Math[roundtag](parseFloat((n * k).toFixed(prec * 2))).toFixed(prec * 2)) / k
    }
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
    var re = /(-?\d+)(\d{3})/
    while (re.test(s[0])) {
        s[0] = s[0].replace(re, '$1' + sep + '$2')
    }

    if ((s[1] || '').length < prec) {
        s[1] = s[1] || ''
        s[1] += new Array(prec - s[1].length + 1).join('0')
    }
    return s.join(dec)
}
//复原为数字
function numberFormatReduce(num){
    if(num&&num!=undefined&&num!=null){
        let _num = num;
        _num = _num.toString();
        _num = _num.replace(/,/gi,'');
        return _num
    }else{
        return num
    }
}

1 列表中的金额

image.png

如图,将计划金额(万元)下的金额变为图中这样

//给表格的tbody上添加一个id
<table class="frog-table">
        <thead>
        <tr>
            <th>序号</th>
            <th>立项号</th>
            <th>项目建议书编号</th>
            <th>项目名称</th>
            <th>资金计划渠道</th>
            <th>计划金额(万元)</th>
            <th>项目状态</th>
            <th>申报人</th>
            <th>申报单位</th>
            <th>立项时间</th>
        </tr>
        </thead>
        <tbody id="cimp1_tbody">
        <c:forEach items="${}" var="" varStatus="">
            <tr target="" rel="${}" onclick="">
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td >${projectamt}</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

在文档中添加一个当文件加载结束后运行的方法,或直接写在script的最后(写在最后是为了防止之前的方法中有要修改数据的)

$(document).ready(function(){
    //通过id找到tbody,遍历tr的每一行
    $("#cimp1_tbody tr").each(function(){
        var trs = $(this).children();
        //将这一行的第6个数字修改为千分符,并保留两位小数
        trs.eq(5).text(numberFormat(trs.eq(5).text(),2))
    });
})

2 表单中的金额

同样在文档中添加一个当文件加载结束后运行的方法,或直接写在script的最后(写在最后是为了防止之前的方法中有要修改数据的)

<script>
    xxx
    xxx
    xxx
    $('#id').val(numberFormat($('#id').val(),2))
</script>

注:表单提交之前,要把表单中内容还原成正常的数字

$('#id').val(numberFormatReduce($('#id').val(),2))

3 在表格中的表单

$("#tabfourtbody tr").each(function () {// tabfourtbody是表格的tobody的id
    var trs = $(this).children();
trs.find("input[name='updateamt']").val((numberFormat(trs.find("input[name='updateamt']").val(),2)));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值