Golang math.Ceil() is a built-in function to find the smallest integer value greater than or equal to a given float64 value. The syntax of the math.Ceil() function is func Ceil(x float64).
The math.Ceil() function rounds up a floating-point number to the nearest integer. For example, math.Ceil(4.1) returns 5 and math.Ceil(-4.1) returns -4.
Syntax
func Ceil(x float64) float64
Parameters
The Ceil() function takes a float64 value as an input parameter.
Return value
The Ceil() function returns a float64 value which is the smallest integer greater than or equal to the input value.
- If you pass +Inf or -Inf in the Ceil() function, it will return +Inf or -Inf.
- If you pass -0 or +0 in the Ceil() function, it will return -0 or +0.
- If you pass NaN in the Ceil() function, it will return NaN.
Example 1: Simple code example
package main
import (
"fmt"
"math"
)
func main() {
data := 19.25
result := math.Ceil(data)
fmt.Println("The Ceiled Value :", result)
}
Output
The Ceiled Value : 20
In this example, the math.Ceil() function takes the floating-point number 19.21 as input and rounds it up to the nearest integer, which is 20. The rounded value is then printed to the console.
Example 2: Another code example
package main
import (
"fmt"
"math"
)
func main() {
data := 2.1
result := math.Ceil(data)
fmt.Println("The Ceiled Value :", result)
}
Output
The Ceiled Value : 3
Example 3: Rounding the infinity using math.Ceil()
package main
import (
"fmt"
"math"
)
func main() {
infty := math.Inf(+1)
fmt.Println("The ceiling value of +Inf is", math.Ceil(infty))
// Negative ∞ infinite value
infnty := math.Inf(-1)
fmt.Println("The ceiling value of -Inf is", math.Ceil(infnty))
}
Output
The ceiling value of +Inf is +Inf
The ceiling value of -Inf is -Inf
Example 4: Rounding the negative value using math.Ceil()
Here is an example of using math.Ceil() function to round a negative value.
package main
import (
"fmt"
"math"
)
func main() {
data := -19.21
result := math.Ceil(data)
fmt.Println("The Rounded Value :", result)
}
Output
The Rounded Value : -19
In this example, use math.Ceil() function to round the negative floating-point number -19.21 to its nearest integer value of -19.
FAQ
How does the math?Ceil() function work in Golang?
The math.Ceil() function rounds the given number x to the nearest integer value greater than or equal to the original number. For example, if the number is 19.21, the rounded value would be 20.
Can the math.Ceil() function round negative values?
Yes, the math.Ceil() function rounds both positive and negative floating-point values.
Is the result of math.Ceil() always a float64 in Golang?
Yes, the result of math.Ceil() function is always a float64 in Golang.
Can math.Ceil() be used with other data types in Golang?
No, math.Ceil() function only works with the float64 data type. To round other data types, you must first convert them to float64 and then use the math.Ceil() function.
Conclusion
Golang math.Ceil() function is used to round a given floating-point number to the nearest integer value that is greater than or equal to the original number.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.