Round()
Published Jul 11, 2023
Contribute to Docs
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:
Contribute to Docs
- 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.
Learn Go on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours