Skip to content

Instantly share code, notes, and snippets.

@jinuljt
Last active August 29, 2015 14:25
Show Gist options
  • Save jinuljt/aa3265583969a8144d99 to your computer and use it in GitHub Desktop.
Save jinuljt/aa3265583969a8144d99 to your computer and use it in GitHub Desktop.
go float 转换 int
/*
由于float精度的问题。在现实世界中遇到下面的问题。
```
f := 4.02
i := int(f * 100.0)
fmt.Println("i = ", i)
```
> i = 401
*/
func Float2Int(f float64) int {
i, _ := strconv.Atoi(strconv.FormatFloat(f, 'f', 0, 64))
return i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment