Welcome To Golang By Example

Ceil of a number in Go (Golang)

Table of Contents

Overview

math package of GO provides a Ceil method that can be used to get the ceil of a number. Ceil of a number is the least integer value greater than or equal to that number.

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

func Ceil(x float64) float64

Some special cases of ceil function are

Code:

package main

import (
    "fmt"
    "math"
)

func main() {
    res := math.Ceil(1.6)
    fmt.Println(res)

    res = math.Ceil(-1.6)
	fmt.Println(res)
    
    res = math.Ceil(1)
    fmt.Println(res)
}

Output:

2
-1
1