淘汰/jQuery货币格式 [英] Knockout/JQuery currency format
本文介绍了淘汰/jQuery货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面的页面上运行得很好(我剪掉了一些其他字段和样式,以使我在此处发布的样本较小).我希望表格中的高级"行(第17行)的格式设置为货币(USD).最好的方法是什么?
I have the below page working pretty well (I've cut out some other fields and styling to keep the sample that I post here smallish). I'd like the Premium line in the table (line 17) to format as currency (USD). What's the best way of doing this?
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="datagrid" >
<table >
<thead>
<tr>
<th>Location Name</th>
<th>Location Total</th>
</tr>
</thead>
<tbody data-bind="foreach: { data: Locations, as: 'location' }">
<tr>
<td><span data-bind="text: location.LocationName" /> </td>
<td><span data-bind="text: location.Premium" /> </td>
</tr>
</tbody>
</table>
</div>
<script>
$(document).ready(function ()
{
var appViewModel
// AppViewModel
function AppViewModel()
{
this.Locations = ko.observable([]);
}
var appViewModel = new AppViewModel();
ko.applyBindings(appViewModel);
$.getJSON("http://waltweb01:85/LTCEPLWS/LTCJSON.svc/getLTCWithIDs/4", function (data)
{
incomingData = data;
appViewModel.Locations(data.getLTCWithIDsResult.Locations);
});
});
</script>
</asp:Content>
推荐答案
答案隐藏在注释中,以便于以后的读者使用,这是一个答案.
Answer is hidden in the comments so to make it easier for future readers here's an answer.
将您的HTML代码更改为:
Change your HTML code to:
<td><span data-bind="text: formatCurrency(location.Premium())" /> </td>
然后添加javascript formatCurrency()函数:
And then add the javascript formatCurrency() function:
var formatCurrency = function (amount) {
if (!amount) {
return "";
}
amount += '';
x = amount.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return "$" + x1 + x2;
}
这篇关于淘汰/jQuery货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文