Golang math.Round() is a built-in function that returns the nearest integer, rounding half away from zero. The syntax of the Round() function is math.Round(x). It takes a floating-point number as an argument and returns the rounded integer value of that number. The returned value is of type float64.
The math.Round() function uses “round half to even” rounding, also known as “bankers rounding”.
In this rounding method, numbers halfway between two integers are rounded to the nearest even integer.
Syntax
func Round(x float64) float64
Arguments
The Round() function accepts a floating-point number as an argument.
Return value
- If you pass -Inf or +Inf in the Round() function like Round(-Inf) or Round(+Inf), it will return -Inf or +Inf.
- If you pass -0 or +0 in the Round() function, like Round(-0) or Round(+0), it will return -0 or +0.
- Passing NaN in the Round() function like Round(NaN) will return NaN.
Example 1
package main
import (
"fmt"
"math"
)
func main() {
data := 19.21
result := math.Round(data)
fmt.Println("The Rounded Value :", result)
}
Output
The Rounded Value : 19
The program declares variable data with a floating-point value of 19.21.
The math.Round() function is then called with the data variable as an argument, and the rounded value is stored in the result variable.
The rounded value is then printed using the fmt.Println() function. In this example, the math.Round() function rounds the floating-point value 19.21 to 19.
Example 2
Let’s execute an example where the floating value is 19.29.
package main
import (
"fmt"
"math"
)
func main() {
data := 19.29
result := math.Round(data)
fmt.Println("The Rounded Value :", result)
}
Output
The Rounded Value : 19
Example 3
Here’s an example of how to pass a negative floating-point number in math.Round() function in Go.
package main
import (
"fmt"
"math"
)
func main() {
data := -19.29
result := math.Round(data)
fmt.Println("The Rounded Value :", result)
}
Output
The Rounded Value : -19
In this example, the math.Round() function will round the negative floating-point number -19.21 to the nearest integer, which is -19.
FAQs
What is the math.Round() function in Golang?
Golang math.Round() is a function that takes a floating-point number as an argument and returns the nearest integer value as a float64.
How does the math.Round() function work in Golang?
The math.Round() function first checks if the decimal value of the floating-point number is greater than or equal to 0.5. If it is, the function rounds up to the next integer. If it’s less than 0.5, it rounds to the nearest integer.
Can you pass a negative floating-point number to the math.Round() function?
Yes, you can pass negative floating-point numbers to the math.Round() function. The math.Round() function works similarly for both positive and negative numbers.
Conclusion
Golang math.Round() function is a useful function for rounding floating-point numbers to the nearest integer. It’s a simple function that requires only a single parameter, the floating-point number you want to round.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.