计算,用逗号替换点 [英] Calculation, replace dot with a comma

查看:108
本文介绍了计算,用逗号替换点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用jQuery Calculation Plugin汇总总数的订单.

I have an order form on which I use the jQuery Calculation Plugin to sum up the total.

这种总结很好用,但是产生的"sum"存在问题.总之,我希望用逗号替换任何点.

This summing up works fine, yet there is a problem with the produced 'sum'. In the sum I wish to replace any dot with a comma.

代码的基础是;

function ($this) {
    var sum = $this.sum();
    $("#totaal").html("€ " + sum2);
}

直接在var sum上使用.replace()无效(引用的函数在对象上不可用).我也尝试过这种方法(但没有效果);

Using a .replace() directly on the var sum doesn't work (referenced function not available on object). I have also tried this (but without effect);

var sum2 = sum.toString().replace(',', '.');

由于我是jQuery的新手,所以现在我很困,有人能指出我正确的方向吗?

As I'm kind of new to jQuery I'm pretty much stuck now, could anyone point me in the right direction?

推荐答案

您的替换行几乎正确.您需要使用带有g选项的正则表达式,该选项表示要替换所有实例而不是仅替换第一个实例.您还交换了订单(第一个是要查找的东西,第二个是要替换的东西).

Your replace line is almost right. You need to use a regexp with the g option, which says to replace all instances instead of just the first. You also have the order swapped (first is what to find, second is what to replace it with).

var sum2 = sum.toString().replace(/\./g, ',');

请注意\之前的\:.在RegExp中具有特殊含义,因此必须对其进行转义.

Note the \ before the .: . has a special meaning in a RegExp, so it has to be escaped.

这篇关于计算,用逗号替换点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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