Floor Value of a Number

To find floor value of a number in Go language, use Floor() function of math package. Floor(x) returns the greatest integer value less than or equal to x.

In this tutorial, we will learn the syntax of Floor() function, and how to use this function to find the floor value of a given number.

Syntax

The syntax of Floor() function is

math.Floor(x)

where x is a floating point value of type float64.

Please note that we have to import “math” package to use Floor() function.

Return Value

The function returns a floating point value of type float64.

Examples

Floor Value of Positive Number

In the following program, we take a float value in x, and find its floor value.

example.go

package main

import (
	"fmt"
	"math"
)

func main() {
	x := 3.14
	result := math.Floor(x)
	fmt.Println("Floor Value :", result)
}

Output

Floor Value : 3

For x = 3.14, 3 is the greater integer less than or equal to 3.14.

Floor Value of Negative Number

In the following program, we take a negative floating value and find its floor value.

example.go

package main

import (
	"fmt"
	"math"
)

func main() {
	x := -3.14
	result := math.Floor(x)
	fmt.Println("Floor Value :", result)
}

Output

Floor Value : -4

Conclusion

In this Golang Tutorial, we learned how to find floor value of a given Float value in Go language, with example programs.