Menu Close

Go – How to get the greatest integer value of number in go

Here, We will learn how to get the greatest integer value of a number in go. We can get it by using Floor() function in math package in go golang.

Function prototype:

func Floor(no float64) float64

Return value:

Floor() function in math package returns
the greatest integer value less than or equal
to number.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Floor(2.51)
  fmt.Printf("%.1f\n", no)

  no = math.Floor(2.39)
  fmt.Printf("%.1f\n", no)

  no = math.Floor(3.12)
  fmt.Printf("%.1f\n", no)

  no = math.Floor(-3.21)
  fmt.Printf("%.1f\n", no)
}

Output:

2.0
2.0
3.0
-4.0

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.

Contact Us