使用 jQuery 计算欧洲格式数字时出现问题
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我成功了。我联系了作者,他给了我以下默认值。您还需要注意的是,总计是通过对行总计进行求和来计算的,因此也要小心地在行总计中打印出正确的小数符号。
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.