take the floor of a number in go

To take the floor of a number in Go, we can use the math.Floor function. This function takes a float64 as input and returns the largest integer value less than or equal to the input.

Here's an example of using math.Floor:

main.go
package main

import (
    "fmt"
    "math"
)

func main() {
    x := 3.14159
    y := math.Floor(x)
    fmt.Println(y) // Output: 3
}
135 chars
13 lines

In this example, we import the math package, which provides a variety of mathematical functions. We then use the math.Floor function to find the largest integer less than or equal to x, and store the result in y. Finally, we print the value of y to the console.

Note that math.Floor returns a float64 value, so if you need an integer result, you can convert the result using a type cast:

main.go
z := int(math.Floor(x))
fmt.Println(z) // Output: 3
52 chars
3 lines

related categories

gistlibby LogSnag