Javascript change commas for points and points for commas? - Stack Overflow

I want to convert all mas in a string in points and vice-versa. However I don't know how to do it

I want to convert all mas in a string in points and vice-versa. However I don't know how to do it since I get all mas or points after the first change.

"1.000,20" should bee "1,000.20"

How can this be done?

I want to convert all mas in a string in points and vice-versa. However I don't know how to do it since I get all mas or points after the first change.

"1.000,20" should bee "1,000.20"

How can this be done?

Share Improve this question edited Jan 20, 2012 at 0:58 lisovaccaro asked Jan 20, 2012 at 0:51 lisovaccarolisovaccaro 34.1k99 gold badges271 silver badges423 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Try

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

which uses the callback function option of replace()

Try this:

var str = "1,000.20";

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

//Iterate through each ponent, replacing mas with periods
var length = ponents.length;

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745118643a4612283.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信