Javascript更改逗号以获得​​逗号的积分和积分?

时间:2012-01-20 00:51:39

标签: javascript

我想用点转换字符串中的所有逗号,反之亦然。但是我不知道该怎么做,因为我在第一次更改后得到所有逗号或分数。

"1.000,20" should become "1,000.20"

如何做到这一点?

2 个答案:

答案 0 :(得分:8)

尝试

"1.000,20".replace(/[\.,]/g, function (m) { return m == '.' ? ',' : '.' })

使用replace()

的回调函数选项

答案 1 :(得分:0)

试试这个:

var str = "1,000.20";

//Split into components based on a period
var components = str.split('.');

//Iterate through each component, replacing commas with periods
var length = components.length;

for (var i = 0; i < length; i++) {
   components[i] = components[i].replace(",", ".");
}

//Array.join can be slow but still useful - join with commas
str = components.join(",");