nin*_*nse 72 jquery currency formatter
我不需要掩码,但我需要一些可以格式化货币的东西(在所有浏览器中)并且不允许输入任何字母或特殊字符.谢谢您的帮助
例:
有效期:$ 50.00
$ 1,000.53
无效:$ w45.00
$ 34.3r6
Mel*_*elu 95
另一种选择(如果你使用的是ASP.Net razor视图),在你的视图中你可以做到
<div>@String.Format("{0:C}", Model.total)</div>
Run Code Online (Sandbox Code Playgroud)
这将正确格式化.note(item.total是double/decimal)
如果在jQuery中你也可以使用Regex
$(".totalSum").text('$' + parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
Run Code Online (Sandbox Code Playgroud)
Rob*_*vey 49
JQUERY FORMATCURRENCY PLUGIN
http://code.google.com/p/jquery-formatcurrency/
Cha*_*ehn 34
扩展Melu的答案,你可以这样做来功能化代码和处理负数.
样本输出:
5.23
美元 - 5.23美元
function formatCurrency(total) {
var neg = false;
if(total < 0) {
neg = true;
total = Math.abs(total);
}
return (neg ? "-$" : '$') + parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString();
}
Run Code Online (Sandbox Code Playgroud)
小智 19
作为jQuery FormatCurrency插件为什么是一个很好的答案的必然结果,我想反驳你的评论:
1. code.google.com/p/jquery-formatcurrency - 不会过滤掉所有字母.您可以键入单个字母,但不会将其删除.
是的,formatCurrency()本身不会过滤掉字母:
// only formats currency
$(selector).formatCurrency();
Run Code Online (Sandbox Code Playgroud)
但是,包含在formatCurrency插件中的toNumber()可以.
你想这样做:
// removes invalid characters, then formats currency
$(selector).toNumber().formatCurrency();
Run Code Online (Sandbox Code Playgroud)
Fer*_*osh 15
使用jquery.inputmask 3.X.在这里看演示
包含文件:
<script src="/assets/jquery.inputmask.js" type="text/javascript"></script>
<script src="/assets/jquery.inputmask.extensions.js" type="text/javascript"></script>
<script src="/assets/jquery.inputmask.numeric.extensions.js" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
和代码一样
$(selector).inputmask('decimal',
{ 'alias': 'numeric',
'groupSeparator': '.',
'autoGroup': true,
'digits': 2,
'radixPoint': ",",
'digitsOptional': false,
'allowMinus': false,
'prefix': '$ ',
'placeholder': '0'
}
);
Run Code Online (Sandbox Code Playgroud)
强调:
小智 7
我曾经使用jquery格式货币插件,但最近一直非常错误.我只需要格式化USD/CAD,所以我编写了自己的自动格式.
$(".currencyMask").change(function () {
if (!$.isNumeric($(this).val()))
$(this).val('0').trigger('change');
$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
});
Run Code Online (Sandbox Code Playgroud)
只需设置任何输入应该格式化为货币的类,<input type="text" class="currencyMask" />它将在任何浏览器中完美地格式化.
| 归档时间: |
|
| 查看次数: |
217104 次 |
| 最近记录: |