jQuery技术:使用带有欧洲格式数字的jQuery计算的问题

我使用jQuery.Calculation插件计算行总计,但总计忽略小数点后的所有内容。 我怀疑修复在于正则表达式,但我无法弄清楚在哪里。

以下方法应设置欧洲小数的默认正确,它适用于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) ); } ); 

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

    我搞定了。 我联系了作者,他给我发了以下默认值。 您还需要注意的是,总计是通过对行总计求和来计算的,因此请务必在行总计中打印出正确的小数符号。

     $.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教程分享使用带有欧洲格式数字的jQuery计算的问题相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/558717.html

      (0)
      上一篇 2021年1月27日
      下一篇 2021年1月27日

      精彩推荐