# How do you round up a float number in Python?

In this article, we will show you how to round up a float number in python.

## Round up float number using the round() function

The round() function gives a floating point number with a specified number of decimals which is the rounded version of the specified number.

The function will return the nearest integer because the default value for the number of decimals is 0.

### Syntax

round(number, digits)


### Parameters

• number(required) − a number that should be rounded

• digits(optional) − up to the number of decimals to be rounded. 0 is the default.

Python has an in-built function round() for this purpose. The function takes two arguments, the number to be rounded and the places up to which it is to be rounded. If the number is to be rounded to the nearest integer, the second argument is not given.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Create a variable to store the input number.

• Use the round() function to round up the input number by passing the input number as an argument.

### Example

The following program returns the rounded-up value of the input number using the round() function −

# input number
inputNum = 2.14357

# rounding up the input number using the round() function
print("rounding up", inputNum,":", round(inputNum))


### Output

On executing, the above program will generate the following output −

rounding up 2.14357 : 2


## Adding 0.5 to the round() function before round up

In this method 0.5 is added to the number before rounding up using the round() function

### Example

# input number
inputNum = 2.14357

# adding 0.5 to the input number and then rounding up using the round() function
print("Adding 0.5 to", inputNum,"and then rounding up:", round(inputNum+0.5))


### Output

On executing, the above program will generate the following output −

Adding 0.5 to 2.14357 and then rounding up: 3


We took a floating point number, say 2.143, then added 0.5 to it (making the number 2.6143>2.5) before passing it as an argument to the round() function. So the round() function rounds this integer to the ceiling in this case because it exceeds half, i.e. 2.5, so the result is 3.

## Round up float number using the ceil() Function

In Python, the method ceil(x) returns the smallest integer greater than or equal to x. It is called the ceiling value of x.

### Syntax

import math
math.ceil(x)


### Parameters

• x − any real number

Return value − Returns the smallest integer not less than x.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword to import the math module.

• Use the math.ceil() function to get the ceiling value of a number i.e, the smallest integer greater than or equal to the number by passing the number as an argument to it.

### Example

The following program returns the round-up value of the given float number in Python. −

# importing math module
import math

# getting the ceiling value of numbers using math.ceil()
print("Round-Up value of -12.11:", math.ceil(-12.11))
print("Round-Up value of 50.26:", math.ceil(50.26))
print("Round-Up value of 30.5:", math.ceil(30.5))
print("Round-Up value of 1.1:", math.ceil(1.1))


### Output

On executing, the above program will generate the following output −

Round-Up value of -12.11: -12
Round-Up value of 50.26: 51
Round-Up value of 30.5: 31
Round-Up value of 1.1: 2


## Round up float number using Boolean Logic

### Example

The following program returns the rounded-up value of the input number using the bool() function −

# input number
n = 3.4

# rounding up the number
print("rounding up the value of", n,":",int(n) + bool(n%1))


### Output

On executing, the above program will generate the following output −

rounding up the value of 3.4: 4


## Round up float number Decimal() function

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Use the import keyword to import all the functions from the decimal module(To import all the functions we use the * operator).

• Use the decimal() function(Gives a decimal value of 50 digits as default) to convert the given number to a decimal number, and then use the quantize() function to quantize it.

### Example

The following program returns the rounded-up value of the input float number using Decimal() function of the decimal module −

# importig all functions from the decimal module
from decimal import *

# rounding up the number
print("the Round up value of 5.834 =",int(Decimal(5.834).quantize(Decimal('1.'), rounding=ROUND_UP)))


### Output

On executing, the above program will generate the following output −

the Round up value of 5.834 = 6


## Round up float number int() function

### Example

The following program returns the rounded-up value of the input float number using the int() function −

# input number
n = 115.914

# rounding up the number
print("Round up value of ",n,"is",int(n) + ((int(n) - n) != 0))


### Output

On executing, the above program will generate the following output −

Round up value of 115.914 is 116


## Round up float using not operator

### Example

The following program returns the rounded−up value of the input float number using int() and is_integer() functions −

# input number
n = 5.3

# rounding up the number
result = int(n) + (not n.is_integer())

# priting the resultant rounded-up number
print("rounding up value of", n,":", result)


### Output

On executing, the above program will generate the following output −

rounding up value of 5.3 : 6


## Conclusion

We covered how to round up a given floating number in Python using multiple ways in this tutorial. By using examples, we also learned about the round() method, which is used to round up a specified integer. We also learned about the decimal module, which allows us to convert a given floating-point number to a decimal and round it up.