Javascript Regex: replacing the last dot for a comma

前端 未结 5 1008
情歌与酒
情歌与酒 2021-01-05 20:38

I have the following code:

var x = \"100.007\"
x = String(parseFloat(x).toFixed(2));
return x
=> 100.01

This works awesomely just how I

5条回答
  •  甜味超标
    2021-01-05 21:26

    You could do it using the lastIndexOf() function to find the last occurrence of the , and replace it.

    The alternative is to use a regular expression with the end of line marker:

    myOldString.replace(/,([^,]*)$/, ".$1");
    

提交回复
热议问题