Created
August 25, 2020 16:22
-
-
Save martinusso/9ca623b04348874926665d3c443fbe9e to your computer and use it in GitHub Desktop.
golang round
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
func Round(input float64) int { | |
var result float64 | |
if input < 0 { | |
result = math.Ceil(input - 0.5) | |
} else { | |
result = math.Floor(input + 0.5) | |
} | |
// only interested in integer, ignore fractional | |
i, _ := math.Modf(result) | |
return int(i) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment