How To Round Up In Python? [Explained]

You can round a number up using the math.ceil() function from the math module.

This function takes a single argument, which is the number you want to round up, and returns the smallest integer greater than or equal to that number. Here’s how you can use it:

import math

number = 5.3
rounded_up = math.ceil(number)

print(rounded_up)  # This will print 6Code language: Python (python)

In this example, math.ceil(5.3) rounds up the number 5.3 to the smallest integer greater than or equal to 5.3, which is 6.

If you want to round up to a specific number of decimal places, you can combine math.ceil() with some arithmetic. For example, if you want to round up to two decimal places:

import math

number = 5.345
rounded_up = math.ceil(number * 100) / 100

print(rounded_up)  # This will print 5.35
Code language: Python (python)

In this case, we multiply the number by 100 to move the desired decimal places to the units position, round up using math.ceil(), and then divide by 100 to get the rounded-up result with two decimal places.

Read More;

  • Dmytro Iliushko

    I am a middle python software engineer with a bachelor's degree in Software Engineering from Kharkiv National Aerospace University. My expertise lies in Python, Django, Flask, Docker, REST API, Odoo development, relational databases, and web development. I am passionate about creating efficient and scalable software solutions that drive innovation in the industry.

Leave a Comment