Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Floor of a number in Go (Golang)

Posted on March 25, 2023March 25, 2023 by admin

Table of Contents

  • Overview
  • Code:

Overview

math package of go provides a Floor method that can be used to get the ceil of a number. Floor of a number is the greatest integer value less than or equal to that number.

Below is the signature of the function. It takes input a float and also returns a float.

func Floor(x float64) float64

Some special cases of floor function are

  • Floor(±0) = ±0
  • Floor(±Inf) = ±Inf
  • Floor(NaN) = NaN

Code:

package main
import (
"fmt"
"math"
)
func main() {
res := math.Floor(1.6)
fmt.Println(res)
res = math.Floor(-1.6)
fmt.Println(res)
res = math.Floor(1)
fmt.Println(res)
}

Output:

1
-2
1
  • go
  • number
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP