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

JavascriptRegexReplaceJscript

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

You can also do it like this:

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

working fiddle example

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeo View Question on Stackoverflow
Solution 1 - JavascriptSmernView Answer on Stackoverflow
Solution 2 - JavascriptMikeyView Answer on Stackoverflow
Solution 3 - JavascriptNaveen Kumar AloneView Answer on Stackoverflow
Solution 4 - JavascriptTom LordView Answer on Stackoverflow
Solution 5 - JavascriptjbabeyView Answer on Stackoverflow
Solution 6 - JavascriptMizzView Answer on Stackoverflow