Home » How to Find Floor of Number in Golang

How to Find Floor of Number in Golang

To find the floor value of a number in go:

  1. Import the math package in the golang program
  2. Use the Floor() function math package
  3. Floor returns the greatest integer value less than or equal to x

In this article, we will discuss how to get the Floor value of a number in golang with examples.

How to Find the Floor Value for a Number in Golang

Use the Floor() function of the math package to find the floor value of a number in golang. It returns the greatest integer value less than or equal to the given number.

Let’s understand how to use Floor() in golang.

package main

import (
	"fmt"
	"math"
)

func main() {
	ans := math.Floor(1.5)
	fmt.Println("Floor of 1.5 is:", ans)

	ans = math.Floor(0)
	fmt.Println("Floor of number 0 is:", ans)

	ans = math.Floor(-1.0578)
	fmt.Println("Floor of -1 is:", ans)

	ans = math.Floor(15.87)
	fmt.Println("Floor of 15.87 is:", ans)

	temp := math.Inf(-1)
	ans = math.Floor(temp)
	fmt.Println("Floor of infinity is:", ans)

}

In the above golang program, we import a math package and used the Floor() method to find the round value to the nearest whole number for a number.

Cool Tip: How to find the log of a number in golang!

Conclusion

I hope the above article helped you to understand how to use the Floor function in golang to find the floor value for a given number.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Leave a Comment