Golang math.Floor() function: How to Use it

Go provides a math package that has functions to find absolute value, power of input, floor value, round value, and ceil values.

In this article, we will find out how to get the floor value of an input.

Golang math.Floor()

The math.Floor() is a built-in Golang function that returns the greatest integer value less than or equal to the specified number. The Floor() function helps us find a decimal number’s rounded-down or floor value.

How to use math.Floor() function?

To use the math.Floor() function, import a math package. Add a math package at the top of your program using the import keyword to access the Floor() function.


func Floor(x float64) float64


The Floor() function accepts only one argument of type float64.

There are some exceptions I would like to mention here.

  1. Inf: If you pass an infinite value(±Inf), the return value will be the same as the argument.
  2. NAN: If you pass a NAN value, it returns NAN.
  3. 0: If you pass the (±)0 argument, it returns the same value as an argument.

Return value

The Floor() function returns a single value of the argument rounded down to the nearest whole number whose data type is float64.

Implementing Floor() function in Go

package main

import (

func main() {
  var floating_point = 19.21
  fmt.Print("The data type is: ", reflect.TypeOf(floating_point), "\n")
  var rounded_down = math.Floor(floating_point)
  fmt.Print("The data type is: ", reflect.TypeOf(rounded_down), "\n")


The data type is: float64
The data type is: float64

You can see that we imported the math package at the top to use its functions and methods.

We declared a float64 value in this example and printed its data type using the reflect.TypeOf() function.

Using math.Floor() function, we rounded a value from 19.21 to 19. Although an output value seems like an integer, it is float64 as we printed its data type after conversion. Therefore, the Floor() function always returns the float64 value.

To convert a float value to int, use the int() function.

Passing Inf, NAN, and 0 to the Floor() function

Inf means infinite value, and NAN means not a number and 0. Inf can be positive or negative.

package main

import (

func main() {
 var infinity = math.Inf(-1)
 var notANumber = math.NaN()
 var zero = 0




It returns the same output as the argument passed to the Floor() function.


To find a rounded-down or the floor value of a decimal number in Golang, use the math.Floor() function.

That’s it.

Leave a Comment