如何用数字中的点替换逗号(或任何替换)

时间:2021-09-20 15:37:36

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

我要替换逗号a点

thanks in advance

谢谢提前

6 个解决方案

#1


65  

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.

当replace()创建/返回一个新字符串而不是修改原来的(tt)时,您需要设置变量(tt)等于从替换函数返回的新字符串。

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

JSFiddle

JSFiddle

#2


8  

You can also do it like this:

你也可以这样做:

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

working fiddle example

工作小提琴的例子

#3


4  

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

在警告框中,它将显示88.9827

#4


2  

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

从函数的定义(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.

replace()方法在字符串中搜索指定值或正则表达式,并返回替换指定值的新字符串。

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.

因此,线:tt。替换(/,/g, '.')不会改变tt的值;它只返回新的值。

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

您需要将这一行替换为:tt = tt。替换(/,/ g,“。”)

#5


1  

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

根据文档,replace返回新的字符串——它不会修改您传递的字符串。

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

#6


-1  

This will need new var ttfixed

这将需要新的var ttfixed

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

然后在tt值插槽下,替换下面所有的指向ttfixed的指针

ttfixed = (tt.replace(",", "."));

#1


65  

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.

当replace()创建/返回一个新字符串而不是修改原来的(tt)时,您需要设置变量(tt)等于从替换函数返回的新字符串。

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

JSFiddle

JSFiddle

#2


8  

You can also do it like this:

你也可以这样做:

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

working fiddle example

工作小提琴的例子

#3


4  

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

在警告框中,它将显示88.9827

#4


2  

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

从函数的定义(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.

replace()方法在字符串中搜索指定值或正则表达式,并返回替换指定值的新字符串。

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.

因此,线:tt。替换(/,/g, '.')不会改变tt的值;它只返回新的值。

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

您需要将这一行替换为:tt = tt。替换(/,/ g,“。”)

#5


1  

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

根据文档,replace返回新的字符串——它不会修改您传递的字符串。

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

#6


-1  

This will need new var ttfixed

这将需要新的var ttfixed

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

然后在tt值插槽下,替换下面所有的指向ttfixed的指针

ttfixed = (tt.replace(",", "."));