Welcome To Golang By Example

Floor of a number in Go (Golang)

Table of Contents

Overview

math package of go provides a Floor method that can be used to get the ceil of a number. Floor of a number is the greatest integer value less than or equal to that number.

Below is the signature of the function. It takes input a float and also returns a float.

func Floor(x float64) float64

Some special cases of floor function are

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Floor(1.6)
    fmt.Println(res)
  
    res = math.Floor(-1.6)
	fmt.Println(res)
    
    res = math.Floor(1)
    fmt.Println(res)
}

Output:

1
-2
1