使用 jQuery 计算欧洲格式数字时出现问题

发布于 2024-09-06 12:17:13 字数 1644 浏览 5 评论 0原文

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

以下方法应设置欧洲小数的默认正确值,它适用于每行,但计算的求和方法忽略小数。下面的正则表达式是官方版本的修复程序,但它仍然无法正常工作。

$.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.Calculation sum 方法计算总计时,结果为 7834,没有小数

这里是计算总计的代码,或多或少直接从示例

$("[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)
                );
            }
        );

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

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.

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, ".");
}
});

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

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

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

发布评论

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

评论(1

悟红尘 2024-09-13 12:17:13

我成功了。我联系了作者,他给了我以下默认值。您还需要注意的是,总计是通过对行总计进行求和来计算的,因此也要小心地在行总计中打印出正确的小数符号。

$.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, ".");
  }
})

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