math.Round() Function in Golang With Examples
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You can round off the given number to the nearest integer(rounding half away from zero) with the help of Round() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access the Round() function.
func Round(a float64) float64
- If you pass -Inf or +Inf in this function like Round(-Inf) or Round(+Inf), then this function will return -Inf or +Inf.
- If you pass -0 or +0 in this function like Round(-0) or Round(+0), then this function will return -0 or +0.
- If you pass NaN in this function like Round(NaN), then this function will return NaN.
Result 1: 37.0 Result 2: -101.0 Result 3: -Inf Result 4: NaN Result 5: +Inf
50.00 + -5.00 = 45.00