如何将这个字符串转换为浮点型? 我想要结果 900.50
case-1: var ConvertThis = '此处任意字符串 900,50 此处也任意字符串';
case-2: var ConvertThis = '此处任意字符串 900.50 此处也任意字符串';
如何做到这一点?
最佳答案
尝试以下代码:
var text = 'any string here 900,50 also any string here';
var matched = text.match(/\d+[,.]\d+/)[0].replace(',', '.');
var num = parseFloat(matched, 10);
console.log(matched);
console.log(num);
打印:
900.50
900.5
关于JavaScript : parse float with string prefix in both start and end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18119526/