Last active
August 29, 2015 14:25
-
-
Save jinuljt/aa3265583969a8144d99 to your computer and use it in GitHub Desktop.
go float 转换 int
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
由于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