Round()
The Round()
function in Go is used to round a given number to the nearest integer. This function is part of the math
package, and the package must be imported to use the function in a Go program.
Syntax
result := math.Round(x)
Where:
x
: is the number to be rounded.result
: is the rounded value returned as a float64.
Example
The following example demonstrates how to use the Round()
function to round a number and print the result:
package mainimport ("fmt""math")func main() {number := 3.7rounded := math.Round(number)fmt.Printf("Rounded value: %.0f\n", rounded)}
The output will be:
Rounded value: 4
Codebyte Example
The following example shows how the Round()
function can be used in a Go program:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.