如何在JS中将字符串(带前缀的数字)转换为Double / Float

时间:2016-09-25 05:52:51

标签: javascript string double

如何将带前缀的数字转换为double/float,例如STA01.021.02

3 个答案:

答案 0 :(得分:3)

使用正则表达式去除非数字(不包括"。")以获得更灵活的解决方案:

parseFloat("STA01.02".replace(/[^0-9\.]+/g, ''));

答案 1 :(得分:0)

如果前缀始终相同......

var str = "STA01.02";
var number = parseFloat(str.substring(3));

答案 2 :(得分:0)

// Assumed "STA0" is the fixed-length prefix, you can adjust the substring at the start you're getting rid of.

var myString = "STA01.02"; 
var noPrefix = myString.substring(4); // Just "1.02" 
var myNumber = parseFloat(noPrefix); 
console.log(myNumber); // Prints 1.02