Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Boostrap表如何在数字之间添加逗号_Javascript_Jquery_Twitter Bootstrap_Twitter Bootstrap 3 - Fatal编程技术网

Javascript Boostrap表如何在数字之间添加逗号

Javascript Boostrap表如何在数字之间添加逗号,javascript,jquery,twitter-bootstrap,twitter-bootstrap-3,Javascript,Jquery,Twitter Bootstrap,Twitter Bootstrap 3,Hi有以下填充引导表的代码 在表格生成过程中,如何在数字之前格式化和添加“$” 任何数字都应按以下顺序显示:$100,00,00.00或$100,00.00或$100.00 这是我的密码 $(function () { $.getJSON("https://api.myjson.com/bins/89vsf", function (jsonFromFile) { $('#table1').bootstrapTable({ data: jsonFromFile.rows

Hi有以下填充引导表的代码

在表格生成过程中,如何在数字之前格式化和添加“$” 任何数字都应按以下顺序显示:
$100,00,00.00或$100,00.00
或$100.00

这是我的密码

$(function () {
$.getJSON("https://api.myjson.com/bins/89vsf", function (jsonFromFile) {
    $('#table1').bootstrapTable({
        data: jsonFromFile.rows
    })
    var data =  $('#table1').bootstrapTable('getData');
    var total1 = data.reduce(function(a, b){

    return a + parseFloat(b.LongMarketValue.replace('$',''));
}, 0);

document.querySelector('.total1').innerHTML = total1;

});
});
HTML表格

<table id="table1">
                    <thead>
                        <tr>
                         <th data-field="state" data-checkbox="true"></th>
                            <th data-field="Account">Account #</th>
                            <th data-field="ClientName">Client</th>
                            <th data-field="AccountType">Account Type</th>
                            <th data-field="MarketValue"> Market Value</th>
                        </tr>
                    </thead>
                      <tfoot>
                    <tr>
                      <td></td>
                      <td></td>
                      <th></th>
                      <th> Total <span class="total1"></span></th>
                    </tr>
                  </tfoot>
                </table>

我在这里使用了几个原型函数

String.prototype.toTwoDecimals = function () {
    var value = this;
    if (isNaN(value)) {
        value = 0;
    }
    return parseFloat(Math.round(value * 100) / 100).toFixed(2);
};

String.prototype.toCommaSeperated = function () {
    var value = this;
    if (isNaN(value)) {
        value = 0;
    }
    while (/(\d+)(\d{3})/.test(value.toString())) {
        value = value.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
    }
    return value;
};

String.prototype.toUSD = function () {
    var value = this;
    if (isNaN(value)) {
        value = 0;
    }
    return '$' + this.toString().toTwoDecimals().toCommaSeperated();
};

然后您可以使用
“10000.ToCommaseOperated()
“100000.ToSD()
您可以使用
toLocaleString()
将数字转换为货币格式。或者使用正则表达式来执行此操作

工作示例

$(函数(){
$.getJSON(“https://api.myjson.com/bins/89vsf,函数(jsonFromFile){
var rows=jsonFromFile.rows.map(函数(项){
item.LongMarketValue=parseFloat(item.LongMarketValue.replace(“$”,“”);
item.LongMarketValue=getFormattedCurrency(item.LongMarketValue);
退货项目;
});
$('#表1')。可引导({
数据:行
})
var data=$('#表1')。bootstrapTable('getData');
var total1=数据减少(函数(a,b){
返回a+parseFloat(b.LongMarketValue.replace(“$”,”).split(“,”).join(“”));
}, 0);
document.querySelector('.total1')。innerHTML=getFormattedCurrency(total1);
});
});
函数getFormattedCurrency(数字){
返回编号。toLocaleString('en-US',{style:'currency',currency:'USD'});
}

帐目#
客户
帐户类型
市场价值
全部的

出于好奇,你为什么要把这些方法放在
字符串上而不是
数字上呢?每次我需要它时,我都有一个字符串。说.toString().tosd()似乎比把所有东西都转换成numbersPSA更有用:只是要小心扩展本机对象原型。这不是禁止的做法,但如果您使用其他第三方库,您可能会遇到意外行为@StackOverflowers I如何转换100000->100,00.00美元。这是使用$($(someVal.toCommaSeparated()).toUSD()的正确方法吗;不完全是,这是一个字符串原型函数,所以可以对任何字符串调用它。它看起来像是一个jquery函数。它应该像
说someVal.toString().tosd()一样简单
或者如果someVal已经是字符串,而不仅仅是
someVal.toUSD()
String.prototype.toTwoDecimals = function () {
    var value = this;
    if (isNaN(value)) {
        value = 0;
    }
    return parseFloat(Math.round(value * 100) / 100).toFixed(2);
};

String.prototype.toCommaSeperated = function () {
    var value = this;
    if (isNaN(value)) {
        value = 0;
    }
    while (/(\d+)(\d{3})/.test(value.toString())) {
        value = value.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
    }
    return value;
};

String.prototype.toUSD = function () {
    var value = this;
    if (isNaN(value)) {
        value = 0;
    }
    return '$' + this.toString().toTwoDecimals().toCommaSeperated();
};