方法:
function stringToFloat(strValue) {
if (strValue.indexOf('.') == -1) {
strValue = strValue + '.';
}
return strValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
return $1 + ',';
}).replace(/\.$/, '');
}
结果:
如果需要保留两位小数,后面加.toFixed(2)就好了。 为什么要加一个点,自己体会吧