【发布时间】:2011-03-06 00:11:16
【问题描述】:
我正在使用jQuery.Calculation plugin 计算总计行,但总计忽略了小数点后的所有内容。我怀疑修复存在于正则表达式中,但我不知道在哪里。
以下方法应为欧洲小数设置默认正确值,它适用于 pr 行,但计算的 sum 方法忽略了小数。下面的正则表达式是对官方版本的修复,但它仍然不能正常工作。
$.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)
);
}
);
使用美国数字样式的默认正则表达式可以正常工作,但我需要计算才能使用 , 作为十进制符号
【问题讨论】:
-
你能展示一些失败的值的例子吗?
-
@Pekka 有些地方使用逗号,而美国人(我敢肯定)使用句号,反之亦然。因此,对于美国人来说,123,456 意味着“十二万三千四百五十六”,而对于欧洲人来说,它意味着“一百二十三千分之四百五十六”。 (您可能知道,但现在所有加入我们的年轻人都在这里 :-)
-
@Pointy 是的,我知道这一点,但很高兴指出。我要的是一些实际数据,因为欧洲甚至存在地区差异:例如德国的
123.456,瑞士的123'456...这是一个很长的故事,通常的答案是,尽快转换成英文:) -
@TT 你能展示一些实际的代码吗,哪些有效,哪些无效?
-
@Pekka:我只是以为您试图将我们转换为英文表示法,但您的意思是在代码中。 ;) 顺便说一句,您可能知道 @TT 什么都不做(但在这种情况下,无论如何都会通知 OP)?
标签: jquery regex jquery-calculation