为什么此Golang代码无法将字符串转换为整数?

为什么此Golang代码无法将字符串转换为整数?

问题描述:

This should have been simple:

strconv.Atoi("1250000.0000")

This results in an error:

0 strconv.ParseInt: parsing "1250000.0000": invalid syntax

Any clues?

这应该很简单: p>

  strconv.Atoi(  “ 1250000.0000”)
  code>  pre> 
 
 

这会导致错误: p>

0 strconv.ParseInt:解析“ 1250000.0000”:语法无效 p> blockquote>

有任何线索吗? p> div>

Atoi works only for strings that can be parsed as integers.

What you need is parseFloat

What dystroy said is true, but keep in mind that floats are inprecise and you could get an incorrect answer that way. In your case you can simply split the string on the period and then use Atoi on that.

strconv.Atoi(strings.Split("1250000.0000", ".")[0])