如何将文本格式设置为货币

发布于 2024-12-27 02:42:18 字数 2797 浏览 0 评论 0原文

我有一个插件,可以通过插入小数点和数字来将数字格式化为货币。千位分隔符:

1000     -> 1.000
10000,5  -> 10.000,5

我这样称呼它:

$('#input').myPlugin() 

但我想通过指定用于小数和千位分隔符的字符来调用它:

$('#input').myPlugin({thousand_type: ',' , decimal_type '.'}). 

如何在我的插件中实现这一点?


这是插件:

(function( $ ){

  $.fn.myPlugin = function() {
    $('input').keypress(function(event){
        if ((event.which  < 48 || event.which  > 57) && event.which  != 8 && event.which  != 44)
        event.preventDefault();
    });
    $('input').keyup(function(event) {
        event.preventDefault();  

        val = $(this).val();

        if(val != ''){
            thousand_type = '.';        
            if(thousand_type == '.'){
                decimal_type = ',';
            }else{
                decimal_type = '.';
            }

            //remove thousand mark
            if(thousand_type == '.'){
                val = String(val).replace(/\./g, "");
            }else{
                val = String(val).replace(/\,/g, "");
            }

            //get position of decimal mark
            pos_decimal = String(val).indexOf(decimal_type);

            //device the number to thousand and decimal
            if(pos_decimal > -1){
                sub_decimal = String(val).substring(pos_decimal, String(val).length);
                sub_thousand = String(val).substring(0, pos_decimal);
            }else{
                sub_decimal = '';
                sub_thousand = val;
            }
            //1.111.111,33
            //remove decimal mark
            if(decimal_type == '.'){
                removed_mark_val = String(val).replace(/\./g, "");
            }else{
                removed_mark_val = String(val).replace(/\,/g, "");
            }

            //check is Numeric
            result = IsNumeric(removed_mark_val);

            if(result == true){
                if(thousand_type == '.'){
                    sub_thousand = String(sub_thousand).split("").reverse().join("")
                      .replace(/(.{3}\B)/g, "$1" + thousand_type)
                      .split("").reverse().join("");
                }else{              
                    sub_thousand = String(sub_thousand).split("").reverse().join("")
                      .replace(/(.{3}\B)/g, "$1" + thousand_type)
                      .split("").reverse().join("");
                }
                val = sub_thousand + sub_decimal;
                //1111111,33
                $(this).attr('value',val);  
            }else{

            }
        }
    });
    function IsNumeric(input){
        var RE = /^-{0,1}\d*\.{0,1}\d+$/;
        return (RE.test(input));
    }
  };
})( jQuery );

I have a plugin that formats numbers as currency, by inserting decimal & thousands separators:

1000     -> 1.000
10000,5  -> 10.000,5

and I call it like this:

$('#input').myPlugin() 

But I want to call it by specifying the characters to use for the decimal and thousands separators:

$('#input').myPlugin({thousand_type: ',' , decimal_type '.'}). 

How can I accomplish that in my plugin?


Here's the plugin:

(function( $ ){

  $.fn.myPlugin = function() {
    $('input').keypress(function(event){
        if ((event.which  < 48 || event.which  > 57) && event.which  != 8 && event.which  != 44)
        event.preventDefault();
    });
    $('input').keyup(function(event) {
        event.preventDefault();  

        val = $(this).val();

        if(val != ''){
            thousand_type = '.';        
            if(thousand_type == '.'){
                decimal_type = ',';
            }else{
                decimal_type = '.';
            }

            //remove thousand mark
            if(thousand_type == '.'){
                val = String(val).replace(/\./g, "");
            }else{
                val = String(val).replace(/\,/g, "");
            }

            //get position of decimal mark
            pos_decimal = String(val).indexOf(decimal_type);

            //device the number to thousand and decimal
            if(pos_decimal > -1){
                sub_decimal = String(val).substring(pos_decimal, String(val).length);
                sub_thousand = String(val).substring(0, pos_decimal);
            }else{
                sub_decimal = '';
                sub_thousand = val;
            }
            //1.111.111,33
            //remove decimal mark
            if(decimal_type == '.'){
                removed_mark_val = String(val).replace(/\./g, "");
            }else{
                removed_mark_val = String(val).replace(/\,/g, "");
            }

            //check is Numeric
            result = IsNumeric(removed_mark_val);

            if(result == true){
                if(thousand_type == '.'){
                    sub_thousand = String(sub_thousand).split("").reverse().join("")
                      .replace(/(.{3}\B)/g, "$1" + thousand_type)
                      .split("").reverse().join("");
                }else{              
                    sub_thousand = String(sub_thousand).split("").reverse().join("")
                      .replace(/(.{3}\B)/g, "$1" + thousand_type)
                      .split("").reverse().join("");
                }
                val = sub_thousand + sub_decimal;
                //1111111,33
                $(this).attr('value',val);  
            }else{

            }
        }
    });
    function IsNumeric(input){
        var RE = /^-{0,1}\d*\.{0,1}\d+$/;
        return (RE.test(input));
    }
  };
})( jQuery );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2025-01-03 02:42:19

如果您想将参数传递给 jQuery 插件,只需接受插件函数的参数即可。

$.fn.myPlugin = function(opts) {

    ...

    if (opts.thousands_type == ',') {

    ...

请参阅此处了解更多信息。

If you want to pass parameters to your jQuery plugin, simply accept parameters to your plugin function.

$.fn.myPlugin = function(opts) {

    ...

    if (opts.thousands_type == ',') {

    ...

See here for more information.

樱&纷飞 2025-01-03 02:42:18

这是我编写的一个插件(缩小为 319 字节):

(function($)
{
    $.fn.myPlugin = function(options)
    {
        options = $.extend({
            thousands: ',',
            decimal: '.'
        }, options);

        return this.keyup(function()
        {
            $(this).val(function(el, val)
            {
                val = val.replace(/[^\d.,]/g, '').split(options.decimal);
                val[0] = val[0].replace(options.decimal === '.' ? /,/g : /\./g, '');
                val[0] = val[0].replace(/(\d)(?=(\d{3})+$)/g, "$1" + options.thousands);
                return val.join(options.decimal);
            });
        });
    };

})(jQuery);

像这样使用它:

$('input').myPlugin({
    thousands: '.',
    decimal: ','
});

在此处查看它的实际操作:http://jsfiddle。 net/yywc7/


注意:这仍然需要一些工作,因为箭头键非常混乱。我在我的项目中使用它,并将插件绑定到 blur 事件中。但由于您似乎希望在 keyup 上使用它,因此需要一些工作才能与箭头键很好地配合。

Here's a plugin I wrote (319 bytes minified):

(function($)
{
    $.fn.myPlugin = function(options)
    {
        options = $.extend({
            thousands: ',',
            decimal: '.'
        }, options);

        return this.keyup(function()
        {
            $(this).val(function(el, val)
            {
                val = val.replace(/[^\d.,]/g, '').split(options.decimal);
                val[0] = val[0].replace(options.decimal === '.' ? /,/g : /\./g, '');
                val[0] = val[0].replace(/(\d)(?=(\d{3})+$)/g, "$1" + options.thousands);
                return val.join(options.decimal);
            });
        });
    };

})(jQuery);

Use it like this:

$('input').myPlugin({
    thousands: '.',
    decimal: ','
});

See it here in action: http://jsfiddle.net/yywc7/


Note: this still needs some work, as the arrow keys are pretty messed up. I use it in my projects with the plugin tying into the blur event. But since you seem to want it on keyup, it needs some work to play nice with the arrow keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文