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 main
import (
"fmt"
"math"
)
func main() {
number := 3.7
rounded := 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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy