Menu Close

Go – Round() fucntion in math package in go

Here, We will learn about Round() fucntion in math package in go.  Round() is used to get the nearest integer, rounding half away from zero.

Function prototype:

func Round(x float64) float64

Return value:

Round() function in math package returns
the nearest integer, rounding half away from zero.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Round(1.2)
  fmt.Println(no)

  no = math.Round(2.7)
  fmt.Println(no)

  no = math.Round(0.0)
  fmt.Println(no)

  no = math.Round(-2.9)
  fmt.Println(no)
}

Output:

1
3
0
-3

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published. Required fields are marked *