60

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

var tt="88,9827";
tt.replace(/,/g, '.')
alert(tt)

//88,9827

i'm trying to replace a comma a dot

thanks in advance

Smern
  • 18,746
  • 21
  • 72
  • 90
Leo
  • 1,787
  • 5
  • 21
  • 43
  • I know it's a super old question, but for those who have complex CSV-like strings my comment might be interesting. If you want to replace commas with dots in a string like this one: "0,001; x,y,z; 0,a;b,1; 2,45;" using the accepted answers won't work. You can then try something like that: ````var res = tt.replace(/([0-9]{1,})(,)([0-9]{1,})/g,"$1.$3");```` This will match the commas only if they are surrounded by at least one number from each side. – Fedor Petrov Sep 30 '20 at 13:03

6 Answers6

120

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

Smern
  • 18,746
  • 21
  • 72
  • 90
  • If you use this for any string, you might run into problems. E.g. inputs like `1, 2, 3` turn into `1. 2. 3`. To make sure you have a number, see solution at https://stackoverflow.com/q/52329376/1066234 – Avatar Sep 14 '18 at 09:53
19

You can also do it like this:

var tt="88,9827";
tt=tt.replace(",", ".");
alert(tt);

working fiddle example

Mikey
  • 191
  • 1
  • 2
5

After replacing the character, you need to be asign to the variable.

var tt = "88,9827";
tt = tt.replace(/,/g, '.')
alert(tt)

In the alert box it will shows 88.9827

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
3

From the function's definition (http://www.w3schools.com/jsref/jsref_replace.asp):

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

This method does not change the original string.

Hence, the line: tt.replace(/,/g, '.') does not change the value of tt; it just returns the new value.

You need to replace this line with: tt = tt.replace(/,/g, '.')

Smern
  • 18,746
  • 21
  • 72
  • 90
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
1

Per the docs, replace returns the new string - it does not modify the string you pass it.

var tt="88,9827";
tt = tt.replace(/,/g, '.');
^^^^
alert(tt);
jbabey
  • 45,965
  • 12
  • 71
  • 94
1

This will need new var ttfixed

Then this under the tt value slot and replace all pointers down below that are tt to ttfixed

ttfixed = (tt.replace(",", "."));
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Mizz
  • 21
  • 1
  • Please take time to improve formatting of your answer. – Xan Nov 05 '14 at 14:11
  • The extra parentheses is unnecessary, and as the OP was hoping to change the original var there is no need to create a new one. Also if you are going to create a new one, you should add `var` in front to avoid creating a global var. – Smern Jun 24 '15 at 13:20
  • This is a very simple yet effective solution. – Alberto Perez Jun 05 '20 at 07:53