Calculation, replace dot with a comma

浪子不回头ぞ 提交于 2019-12-03 10:30:57

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, ',');

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

If Sum was a number then this would work.

var sum_formatted = String( sum ).replace(/\./g,',');

Can you run typeof(sum) and tell us what the output is.

Also if you can set the project up in jsfiddle.com that would be great.

Your problem is that your replace function should read replace('.', ',') not the other way around (you had replace(',', '.')), Note that the first argument is what you're looking for, and the second argument is what you want there instead. You were replacing all commas with periods. Regex here is unnecessary.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!