javascriptregexreplacejscript

How to replace a comma with a dot in a number (or any replacement)


I have not found a solution yet for replacing , with a dot.

My current code:

var tt="88,9827";
tt.replace(/,/g, '.')
console.log(tt)
//expected 88.9827
//returns 88,9827


Solution

  • As replace() creates/returns a new string rather than modifying the original (tt), you need to set the variable (tt) equal to the new string returned from the replace function.

    tt = tt.replace(/,/g, '.')
    

    JSFiddle