The round() function in Python is a built-in function used to round a number to the nearest integer or to a specified number of decimal places.
Basic Usage:
round(number)rounds to the nearest integer. For example,round(3.7)returns4, andround(3.2)returns3.Decimal Places:
round(number, ndigits)rounds tondigitsdecimal places. For example,round(3.14159, 2)returns3.14.Bankers' Rounding: When a number is exactly halfway between two integers (e.g.,
2.5or3.5), Python uses "round half to even" โ rounding to the nearest even number. For example,round(2.5)returns2, andround(3.5)returns4.Negative
ndigits: You can round to the nearest 10, 100, etc., by using a negativendigits. For example,round(1234, -2)returns1200(nearest hundred).Return Type: If
ndigitsis omitted or0,round()returns an integer. Otherwise, it returns a float.Floating-Point Precision: Due to how floating-point numbers are stored, some results may be unexpected (e.g.,
round(2.675, 2)returns2.67, not2.68). For high-precision tasks like financial calculations, use thedecimalmodule instead.
For rounding up or down specifically, use math.ceil() (always round up) or math.floor() (always round down). To round numbers in a list or array, use list comprehensions or numpy.round().
The math.ceil (ceiling) function returns the smallest integer higher or equal to x.
For Python 3:
import math
print(math.ceil(4.2))
For Python 2:
import math
print(int(math.ceil(4.2)))
Answer from Steve Tjoa on Stack OverflowHow does round function handle scientific notation?
python - How do you round UP a number? - Stack Overflow
python - How to properly round-up half float numbers? - Stack Overflow
Given that round(0.5)==0 in Python, how do I round correctly?
http://docs.python.org/library/decimal.html
from decimal import *
number = Decimal('0.5')
print round(number)
is one way to do it.
More on reddit.comVideos
The math.ceil (ceiling) function returns the smallest integer higher or equal to x.
For Python 3:
import math
print(math.ceil(4.2))
For Python 2:
import math
print(int(math.ceil(4.2)))
I know this answer is for a question from a while back, but if you don't want to import math and you just want to round up, this works for me.
>>> int(21 / 5)
4
>>> int(21 / 5) + (21 % 5 > 0)
5
The first part becomes 4 and the second part evaluates to "True" if there is a remainder, which in addition True = 1; False = 0. So if there is no remainder, then it stays the same integer, but if there is a remainder it adds 1.
The Numeric Types section documents this behaviour explicitly:
round(x[, n])
x rounded to n digits, rounding half to even. If n is omitted, it defaults to 0.
Note the rounding half to even. This is also called bankers rounding; instead of always rounding up or down (compounding rounding errors), by rounding to the nearest even number you average out rounding errors.
If you need more control over the rounding behaviour, use the decimal module, which lets you specify exactly what rounding strategy should be used.
For example, to round up from half:
>>> from decimal import localcontext, Decimal, ROUND_HALF_UP
>>> with localcontext() as ctx:
... ctx.rounding = ROUND_HALF_UP
... for i in range(1, 15, 2):
... n = Decimal(i) / 2
... print(n, '=>', n.to_integral_value())
...
0.5 => 1
1.5 => 2
2.5 => 3
3.5 => 4
4.5 => 5
5.5 => 6
6.5 => 7
For example:
from decimal import Decimal, ROUND_HALF_UP
Decimal(1.5).quantize(0, ROUND_HALF_UP)
# This also works for rounding to the integer part:
Decimal(1.5).to_integral_value(rounding=ROUND_HALF_UP)