Tuesday, July 20, 2010

Make Python Round Up

The Python programming language includes an integrated rounding function known as "round()." This function rounds both way, depending on whether the decimal value is above or below .6. The only way to always round up in Python is by importing the "math" module, as it contains a function that will return the smallest integer value that is either equal to or greater than the initial value.


Instructions


1. Import the "math" module:


import math


2. Initialize a numerical variable to store the result if you plan to separate the rounded value from the original value:


rounded_value = 0


3. Pass the original value through the "ceil" function and assign it to the "rounded_value" variable:


rounded_value = math.ceil(original_value)


Replace "original_value" with the variable whose value you need to round.

Tags: math module, original value