2012-01-20 52 views

回答

8

嘗試

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

它使用的replace()

+0

更聰明的答案:)我在想一個正則表達式的解決方案,但忘記了回調功能。做得很好 –

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(",");