How to round a float number up in python ?

Published: June 19, 2023

Tags: Python; Math;

DMCA.com Protection Status

Rounding up numbers in Python can be done using the built-in function math.ceil():

Using math.ceil() function

The math.ceil() function rounds a number up to the nearest integer:

import math

math.ceil(number)

Example with x = 7.2

import math

x = 7.2

print( math.ceil(x) )

Ouput

8

Example with x = 7.7

import math

x = 7.7

print( math.ceil(x) )

Ouput

8

Example with x = 7.0

Note that

import math

x = 7.0

print( math.ceil(x) )

Ouput

7

Without using math module

Round a number up without using math module:

x = 7.7

if int(x) - x < 0:
    print( int(x) + 1 )
else:
    print( int(x) )

returns

8

References

Links Site
math.ceil(x) docs.python.org
round() docs.python.org