What is the Ceil function in golang?

The Go programming language uses the Ceil function to find the rounded-up or the ceiling value of a decimal number.

To use this function, you must import the math package in your file and access the Ceil function within it using the . notation (math.Ceil). Here, Ceil is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Ceil function inside the math package is:

Parameters

Ceil function takes a single argument of type float64. This argument represents the number you need to round up.

Return value

The Ceil function returns a single value of type float64. This value represents the value of the argument rounded up to the nearest whole number.

An exception to the above statements is when you pass as an argument something that is infinity, NAN or 0:

  • (±)Inf: If the argument has an infinite value the return value will be exactly the same as the argument.

  • NAN: If a NAN argument is passed then the return value is also NAN.

  • (±)0: If the argument passed is (±)0the return value will be exactly the same as the argument.

Examples

Following is a simple example where we find out the ceiling value of 25.1:

package main
import (
"fmt"
"math"
)
func main() {
x := 25.1
y := math.Ceil(x)
fmt.Print(x, " is rounded up to ", y, " using Ceil")
}

The following example shows how the Ceil function handles infinite valued arguments, for which we use the Inf function.

The Inf function returns an infinite value with a sign matching the sign of the argument that it is given.

package main
import (
"fmt"
"math"
)
func main() {
x := math.Inf(-1)
y := math.Ceil(x)
fmt.Print(x, " is rounded up to ", y, " using Ceil")
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved