What is the Floor function in golang?

The Go programming language uses the Floor function to find the rounded-down or the floor value of a decimal number.

To use this function, you must import the math package in your file and access the Floor function within it using the . notation (math.Floor). Here, Floor is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Floor function inside the math package is:

Parameters

Floor function takes a single argument of type float64. This argument represents the number you need to round down.

Return value

The Floor function returns a single value of type float64. This value represents the value of the argument rounded down to the nearest whole number.

An exception to the above statements is when you pass as an argument something that is infinity, NAN or 0:

  • (±)Inf: If the argument has an infinite value, the return value will be exactly the same as the argument.

  • NAN: If a NAN argument is passed, the return value is also NAN.

  • (±)0: If the argument passed is (±)0the return value will be exactly the same as the argument.

Examples

Following is a simple example where we find out the floor value of 25.1:

package main
import (
"fmt"
"math"
)
func main() {
x := 25.1
y := math.Floor(x)
fmt.Print(x, " is rounded down to ", y, " using Floor")
}

The following example shows how the Floor function handles infinite valued arguments, for which we use the Inf function:

The Inf function returns an infinite value with a sign matching the sign of the argument that it is given.

package main
import (
"fmt"
"math"
)
func main() {
x := math.Inf(-1)
y := math.Floor(x)
fmt.Print(x, " is rounded down to ", y, " using Floor")
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved