Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 将数字和字母混合的字符串转换为数字_Go - Fatal编程技术网

Go 将数字和字母混合的字符串转换为数字

Go 将数字和字母混合的字符串转换为数字,go,Go,我很难将字母和数字混合的字符串转换为带(或不带)小数的数字。字符串不一定是99,50,但也可以是表示数字的任何其他字符串。十进制分隔符也可以是,而不是, 我尝试了以下方法(): 我从他们那里得到的结果是: 0 我想要的输出是: 99.50 也可以接受的是: 99 您可以使用regexp包删除字母并替换,为,使用ReplaceAll并解析 price := "99,50 SEK" reg, _:= regexp.Compile("[^0-9,]+") // regex for dig

我很难将字母和数字混合的字符串转换为带(或不带)小数的数字。字符串不一定是
99,50
,但也可以是表示数字的任何其他字符串。十进制分隔符也可以是
,而不是

我尝试了以下方法():

我从他们那里得到的结果是:

0
我想要的输出是:

99.50
也可以接受的是:

99

您可以使用
regexp
包删除字母并替换
,使用
ReplaceAll
并解析

price := "99,50 SEK"    
reg, _:= regexp.Compile("[^0-9,]+") // regex for digit and comma only
processedString := reg.ReplaceAllString(price , "") // remove all letters 
processedString = strings.ReplaceAll(processedString, ",", ".") // replace comma with point

res3, _ := strconv.ParseFloat(processedString, 64)

操场中的代码

我甚至建议使用(\d+),(\d+)reg.FindAllSubmatch..直接提取数字。@如果NOTAK问题是和浮点值需要解析
price := "99,50 SEK"    
reg, _:= regexp.Compile("[^0-9,]+") // regex for digit and comma only
processedString := reg.ReplaceAllString(price , "") // remove all letters 
processedString = strings.ReplaceAll(processedString, ",", ".") // replace comma with point

res3, _ := strconv.ParseFloat(processedString, 64)