Finding Floor Value of a Given Number in Golang


In Golang, the math.Floor() function is used to find the largest integer value less than or equal to a given float64 value. The function returns a float64 value.

In this article, we will discuss how to find the floor value of a given number in Golang using the math.Floor() function.

Syntax

The syntax for using the math.Floor() function is as follows −

func Floor(x float64) float64

The Floor() function takes a single argument x of type float64 and returns a float64 value.

Parameters

The Floor() function takes a single parameter x of type float64. This parameter represents the number whose floor value is to be found.

Return Value

The Floor() function returns a float64 value, which represents the largest integer value less than or equal to the given float64 value.

Examples

Here are some examples of using the math.Floor() function to find the floor value of a given number −

Example 1: Finding the Floor Value of a Positive Number

package main

import (
   "fmt"
   "math"
)

func main() {
   num := 7.8
   floorVal := math.Floor(num)
   fmt.Println("Floor value of", num, "is", floorVal)
}

Output

Floor value of 7.8 is 7

Example 2: Finding the Floor Value of a Negative Number

package main

import (
   "fmt"
   "math"
)

func main() {
   num := -3.2
   floorVal := math.Floor(num)
   fmt.Println("Floor value of", num, "is", floorVal)
}

Output

Floor value of -3.2 is -4

Example 3: Finding the Floor Value of Zero

package main

import (
   "fmt"
   "math"
)

func main() {
   num := 0.0
   floorVal := math.Floor(num)
   fmt.Println("Floor value of", num, "is", floorVal)
}

Output

Floor value of 0 is 0

Conclusion

In this article, we discussed how to find the floor value of a given number in Golang using the math.Floor() function. The math.Floor() function takes a single argument of type float64 and returns a float64 value. It returns the largest integer value less than or equal to the given float64 value.

Updated on: 12-Apr-2023

456 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements