Quantcast
Viewing all articles
Browse latest Browse all 25

Python ceil method for numbers

In this lesson we will learn about Python ceil method for numbers. This method will round a number upwards to it’s nearest integer value.

Python ceil method

Python ceil method returns ceiling value of a number a and the smallest integer will still not be less than the number a.

Python ceil method is in the math module. So you can not access it directly. You will have to import math module first and then you can call this method using the math object.

Syntax

The syntax for Python ceil method is:

math.ceil( x )

You will also have to import math module first. So the complete syntax is:

import math
math.ceil( myNum )

  • myNum – This is the number whose value will be used for ceiling.

This method will return an integer value.

Example

The following example shows the usage of ceil() method.

import math

print "ceil for 99.75 : ", math.ceil(99.75)
print "ceil for -66.67 : ", math.ceil(-66.67)

print "ceil for 33.33 : ", math.ceil(33.33)
print "ceil for 0.52 : ", math.ceil(0.52)

print "ceil for math.pi : ", math.ceil(math.pi)
print "ceil for math.e : ", math.ceil(math.e)

If you run the above code then the output will be something like this:

ceil for 99.75 : 100.0
ceil for -66.67 : -66.0
ceil for 33.33 : 34.0
ceil for 0.52 : 1.0
ceil for math.pi : 4.0
ceil for math.e : 3.0

 

The post Python ceil method for numbers appeared first on JS Tricks.


Viewing all articles
Browse latest Browse all 25

Trending Articles