Golang math.Round() function is “used to find the nearest integer, rounding half away from zero”. 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.
Syntax
func Round(x float64) float64
Parameters
x: 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
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
That’s it.
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.