简体   繁体   English

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

[英]Why is this Golang code to convert a string to an integer failing?

This should have been simple: 这应该很简单:

strconv.Atoi("1250000.0000")

This results in an error: 这会导致错误:

0 strconv.ParseInt: parsing "1250000.0000": invalid syntax 0 strconv.ParseInt:解析“ 1250000.0000”:无效的语法

Any clues? 有什么线索吗?

Atoi works only for strings that can be parsed as integers. Atoi仅适用于可解析为整数的字符串。

What you need is parseFloat 您需要的是parseFloat

What dystroy said is true, but keep in mind that floats are inprecise and you could get an incorrect answer that way. dystroy说的是正确的,但请记住,浮子不准确,这样您可能会得到错误的答案。 In your case you can simply split the string on the period and then use Atoi on that. 在您的情况下,您可以简单地在句点上分割字符串,然后在其上使用Atoi

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM