doulangtiao4508
doulangtiao4508
2013-10-07 00:09
浏览 87
已采纳

如何将字符串转换为浮点数

http://play.golang.org/p/7kR2uZlV5-

This is my playground link. I have an array of numbers but in string. I tried to convert them to float number but it does not give me anything. What is wrong with it?

var numbers []float64

for _, elem := range str_numbers {
    i, err := strconv.ParseFloat(elem, 64)
    if err != nil {
        numbers = append(numbers, i)
    }
}
fmt.Println(numbers)    
    // this gives me nothing  []

图片转代码服务由CSDN问答提供 功能建议

http://play.golang.org/p/7kR2uZlV5-

这是我的游乐场链接。 我有一个数字数组,但是是字符串。 我试图将它们转换为浮点数,但没有任何帮助。 怎么了

  var number [] float64 
 
for _,elem:= range str_numbers {
i,err:= strconv.ParseFloat(elem,64)
 if err!  = nil {
数字= append(数字,i)
} 
} 
fmt.Println(数字)
 //这不会给我任何东西[] 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzou8074
    douzou8074 2013-10-07 00:14
    已采纳

    Change

    if err != nil {
    

    to

    if err == nil {
    

    (You may be doing this already, but unit testing is a great way to catch bugs like this.)

    点赞 评论

相关推荐