Home »
Golang »
Golang Reference
Golang math.Ceil() Function with Examples
Golang | math.Ceil() Function: Here, we are going to learn about the Ceil() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 29, 2021
math.Ceil()
The ceiling is often used as a rounding function. It is useful when we want to get the closest integer greater than or equal to a given number. This is a single-value function.
The Ceil() function is an inbuilt function of the math package which is used to get the least integer value greater than or equal to the given value.
It accepts one parameter and returns the ceiling value.
Syntax
func Ceil(x float64) float64
Parameters
- x : The value whose ceiling value is to be found.
Return Value
The return type of Ceil() function is a float64, it returns the least integer value greater than or equal to the given value.
Special cases are:
- Ceil(±0) = ±0
If the parameter is ±0, it returns the same (±0).
- Ceil(±Inf) = ±Inf
If the parameter is ±Inf, it returns the same (±Inf).
- Ceil(NaN) = NaN
If the parameter is NaN (Not-A-Number), it returns the same (NaN).
Example 1
// Golang program to demonstrate the
// example of math.Ceil() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Ceil(1.49))
fmt.Println(math.Ceil(2.50))
fmt.Println(math.Ceil(2.51))
fmt.Println(math.Ceil(-1.49))
fmt.Println(math.Ceil(-2.50))
fmt.Println(math.Ceil(-2.51))
fmt.Println(math.Ceil(-0))
fmt.Println(math.Ceil(+0))
fmt.Println(math.Ceil(math.Inf(-1)))
fmt.Println(math.Ceil(math.Inf(1)))
fmt.Println(math.Ceil(math.NaN()))
}
Output:
2
3
3
-1
-2
-2
0
0
-Inf
+Inf
NaN
Example 2
// Golang program to demonstrate the
// example of math.Ceil() Function
package main
import (
"fmt"
"math"
)
func main() {
var x float64
var CeilX float64
x = 1.49
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = 2.50
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = 3.65
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = -1.49
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = -2.50
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = -3.65
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = 0
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
x = math.Inf(1)
CeilX = math.Ceil(x)
fmt.Println("Ceiling value of of", x, "is", CeilX)
}
Output:
Ceiling value of of 1.49 is 2
Ceiling value of of 2.5 is 3
Ceiling value of of 3.65 is 4
Ceiling value of of -1.49 is -1
Ceiling value of of -2.5 is -2
Ceiling value of of -3.65 is -3
Ceiling value of of 0 is 0
Ceiling value of of +Inf is +Inf
Golang math Package Constants and Functions »