使用带有欧洲格式数字的jQuery计算时出现问题 [英] Problem using jQuery calculation with European-formatted numbers

查看:118
本文介绍了使用带有欧洲格式数字的jQuery计算时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery.Calculation插件计算总计的行,但总数忽略了小数点后的所有内容.我怀疑解决方法在于正则表达式,但我不知道在哪里.

I am calculating rows with a total with the jQuery.Calculation plugin but the total is ignoring everything after the decimal symbol. I suspect the fix lies in the regex but I can't figure out where.

以下方法应为欧洲十进制设置正确的默认值,它适用于pr行,但求和方法将忽略十进制数字.以下正则表达式是官方版本的修复程序,但仍无法正常工作.

The following method should set the default correct for European decimals, it works pr row but the sum method for the calculation is ignoring the decimal numbers. The regex below is a fix for the official release but it is still not working correct.

$.Calculation.setDefaults({
    reNumbers: /(-|-\$)?(\d+(.\d{3})*(\,\d{1,})?|\.\d{1,})/g
, cleanseNumber: function (v) {
    return v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".");
}
});

示例: 7834,45 * 1为该行给出了7834,45的正确总和,但是当使用jQuery计算总和时.计算总和方法得出的结果为7834,没有小数位

Example: 7834,45 * 1 gives the correct sum of 7834,45 for that line, but when calculating the total using jQuery.Calculation sum method this comes out as 7834 with no decimals

这是计算总数的代码,从示例中直接或多或少会撕掉

Here is the code calculating the totals, more or less ripped straight from the examples

$("[id^=total_umva_item_]").calc(
    // the equation to use for the calculation
            "qty * price",
    // define the variables used in the equation, these can be a jQuery object
            {
            qty: $("input[name^=antall_]"),
            price: $("input[name^=price_]")
        },
    // define the formatting callback, the results of the calculation are passed to this function
            function (s) {
                // return the number as a dollar amount
                return "kr " + s.toFixed(2);
            },
    // define the finish callback, this runs after the calculation has been complete
            function ($this) {
                // sum the total of the $("[id^=total_item]") selector
                var sum = $this.sum(); <- The sum is calculated without decimals
                $("#invoice-totals-net").text(
                    "kr " + sum.toFixed(2)
                );
            }
        );

使用美国数字样式的默认正则表达式可以正常工作,但我需要将,作为小数点符号进行计算

Using the default regex for US number style this work correctly but I need the calculations to work with , as the decimal symbol

推荐答案

我知道了.我联系了作者,他将下面的默认值传递给了我.您还需要注意的是,总计是通过对行总计求和而得出的,因此请小心在行总计中打印出正确的小数点符号.

I got it working. I contacted the author and he passed me the defaults below. What you need to be aware of too is that the grand total is calculated by summing the row totals so be careful to print out the correct decimal symbol in the row totals as well.

$.Calculation.setDefaults({
  // a regular expression for detecting European-style formatted numbers

  reNumbers: /(-|-\$)?(\d+(\.\d{3})*(\,\d{1,})?|\,\d{1,})?/g
  // define a procedure to convert the string number into an actual usable number
  , cleanseNumber: function (v){
     // cleanse the number one more time to remove extra data (like commas and dollar signs)
     // use this for European numbers: v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".")

     return v.replace(/[^0-9,\-]/g, "").replace(/,/g, ".");
  }
})

这篇关于使用带有欧洲格式数字的jQuery计算时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆