2010-03-01 14:40:04 8 Comments

This problem is killing me. How does one roundup a number UP in Python?

I tried round(number) but it round the number down. Example:

```
round(2.3) = 2.0 and not 3, what I would like
```

The I tried int(number + .5) but it round the number down again! Example:

```
int(2.3 + .5) = 2
```

Then I tried round(number + .5) but it won't work in edge cases. Example:

```
WAIT! THIS WORKED!
```

Please advise.

### Related Questions

#### Sponsored Content

#### 65 Answered Questions

### [SOLVED] Round to at most 2 decimal places (only if necessary)

**2012-08-06 17:17:48****stinkycheeseman****1963831**View**2221**Score**65**Answer- Tags: javascript decimal rounding decimal-point

#### 26 Answered Questions

### [SOLVED] How can I safely create a nested directory?

**2008-11-07 18:56:45****Parand****2268087**View**3568**Score**26**Answer- Tags: python exception path directory operating-system

#### 21 Answered Questions

### [SOLVED] Does Python have a ternary conditional operator?

**2008-12-27 08:32:18****Devoted****1676490**View**5190**Score**21**Answer- Tags: python operators ternary-operator conditional-operator python-2.5

#### 64 Answered Questions

#### 15 Answered Questions

### [SOLVED] What are metaclasses in Python?

**2008-09-19 06:10:46****e-satis****699419**View**5128**Score**15**Answer- Tags: python oop metaclass python-datamodel

#### 9 Answered Questions

### [SOLVED] Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

**2015-05-06 15:32:43****Rick Teachey****148032**View**1668**Score**9**Answer- Tags: python performance python-3.x range python-internals

#### 56 Answered Questions

### [SOLVED] Calling an external command in Python

**2008-09-18 01:35:30****freshWoWer****3030920**View**4238**Score**56**Answer- Tags: python shell command subprocess external

#### 24 Answered Questions

### [SOLVED] How do I parse a string to a float or int in Python?

**2008-12-19 01:52:26****Tristan Havelick****3517983**View**1998**Score**24**Answer- Tags: python parsing floating-point type-conversion integer

#### 16 Answered Questions

### [SOLVED] Convert bytes to a string?

**2009-03-03 12:23:01****Tomas Sedovic****1646090**View**1667**Score**16**Answer- Tags: python string python-3.x

## 24 comments

## @Timo-m 2019-04-05 09:43:36

int() will always take the integer of the value and therefore always "round down", so to round up on integers one may use:

As suggested before the math.ceil function would word, moreover numpy.ceil also exists, when you prefer to work with numpy.

## @James Coyle 2019-04-05 10:33:14

That breaks for values that are already integers.

## @andres 2018-11-21 15:02:57

My share

I have tested

`print(-(-101 // 5)) = 21`

given example above.Now for rounding up:

I can not use

`**`

so I spread the multiply to division:## @PonasM 2018-06-21 14:16:13

This function requires no modules.

## @Bolboa 2018-10-20 23:01:35

What if your number is

`3`

, then it would round up to`4`

which may or may not be what someone wants## @Nearoo 2019-01-06 18:47:14

This only works in 99% of all cases. You didn't think this through properly. Such solutions should be avoided at all costs.

## @Pavel 2018-03-16 23:52:26

For those who want to round up

`a / b`

and get integer:Another variant using integer division is

## @SooBin Kim 2018-03-14 19:12:17

I think you are confusing the working mechanisms between

`int()`

and`round()`

.`int()`

always truncates the decimal numbers if a floating number is given; whereas`round()`

, in case of`2.5`

where`2`

and`3`

are both within equal distance from`2.5`

, Python returns whichever that is more away from the 0 point.## @Nearoo 2018-06-19 12:43:49

"rounding up" means that e.g.

`2.3`

gets turned into`3`

, which happens in neither of your examples.## @Steve Tjoa 2010-03-01 14:40:58

The ceil (ceiling) function:

## @R. W. Sinnet 2015-08-26 23:37:05

Elaboration: math.ceil returns the smallest integer which is greater than or equal to the input value. This function treats the input as a float (Python does not have strongly-typed variables) and the function returns a float. If you want an int, you can construct an int from the return value, i.e.,

`int(math.ceil(363))`

## @TheEspinosa 2016-01-08 12:54:10

@Sinnet: Actually one could say that python is strongly typed stackoverflow.com/a/11328980/5069869

## @quamrana 2017-12-17 12:27:22

@TheEspinosa: Yes, python is definitely

stronglytyped, its just that many functions ask questions about the type of some parameters and execute different code depending on the answer.## @Arthur Tacca 2018-05-02 15:50:12

@R.W.Sinnet In Python 3,

`math.ceil`

returns an actual integer object, not just floating object with integer value.## @TrophyGeek 2011-10-31 06:39:44

Interesting Python 2.x issue to keep in mind:

The problem is that dividing two ints in python produces another int and that's truncated before the ceiling call. You have to make one value a float (or cast) to get a correct result.

In javascript, the exact same code produces a different result:

## @gecco 2011-10-31 06:46:58

In

Python 2.x: int/int --> intandint/float --> float InPython 3.x: int/int can result in a float## @Rob Dennis 2013-10-22 17:54:56

you can get the Python 3.x on behavior on certain versions of Python 2.x by enabling "true division" as shown here

## @user3712978 2017-12-04 22:22:47

Try this:

## @Tom Aranda 2017-12-04 23:01:25

Clever. The

`((int(a) - a) != 0)`

expression returns`1`

whenever`a`

needs to be rounded up. You may want to expand your answer and explain how this work.## @Bowen Liu 2018-12-18 20:51:06

@TomAranda Can anyone explain how a boolean expression evaluates to a value please?

## @Tom Aranda 2018-12-18 23:46:55

That is a better question for the OP. I just edited for code formatting.

## @Kuřátko Zvyk 2017-11-19 12:09:29

Without importing math // using basic envionment:

a) method / class method

b) lambda:

## @Sebin 2017-03-19 13:49:15

If you don't want to import anything, you can always write your own simple function as:

`def RoundUP(num): if num== int(num): return num return int(num + 1)`

## @Johannes Maria Frank 2017-03-28 00:01:12

This does not work if num is 2.05. You have to have at least as many digits with a 9 as your input, leaving you with a 0.999... which is 1. But then your corner case 2 is rounded up again. -- Well, I guess there is a reason why math.ceil is there.

## @Nearoo 2017-05-05 03:23:21

The above answers are correct, however, importing the

`math`

module just for this one function usually feels like a bit of an overkill for me. Luckily, there is another way to do it:`True`

and`False`

are interpreted as`1`

and`0`

in a statement involving numbers in python.`g.is_interger()`

basically translates to`g.has_no_decimal()`

or`g == int(g)`

. So the last statement in English reads`round g down and add one if g has decimal`

.## @Nearoo 2017-05-05 03:23:45

And if you feel fancy, you can use

`int(g) + (g % 1 > 0)`

instead ;-)## @SH7890 2019-01-25 21:50:57

`from math import ceil`

seems to fix importing the entire math module :)## @Nearoo 2019-01-27 16:52:06

@SH7890 I'm afraid that line isn't much different to

`import math`

in terms of what happens behind the scenes. It just drops all symbols except`ceil`

.## @erick vicente 2017-04-27 16:46:36

when you operate 4500/1000 in python, result will be 4, because for default python asume as integer the result, logically: 4500/1000 = 4.5 --> int(4.5) = 4 and ceil of 4 obviouslly is 4

using 4500/1000.0 the result will be 4.5 and ceil of 4.5 --> 5

Using javascript you will recieve 4.5 as result of 4500/1000, because javascript asume only the result as "numeric type" and return a result directly as float

Good Luck!!

## @Nearoo 2018-06-19 12:41:30

That's only true in Python 2.x. In Python 3, division with a single

`/`

always results in a float, so`4500/1000`

is always 4.5.## @Andreas Schuh 2017-04-08 09:52:00

I am surprised nobody suggested

for integer division with rounding up. Used to be the common way for C/C++/CUDA (cf.

`divup`

)## @Bharel 2017-07-06 12:31:19

Relevant only for statically typed languages. If the denominator is a float you're dead.

## @kennytm 2010-03-01 14:41:23

Use

`math.ceil`

to round up:NOTE: The input should be float.If you need an integer, call

`int`

to convert it:BTW, use

`math.floor`

to rounddownand`round`

to round to nearest integer.## @guival 2017-02-24 10:33:26

The input does not necessarily need to be a float if using python 3:

`ceil()`

will take care of it internally## @Klik 2016-09-08 06:31:20

I'm surprised I haven't seen this answer yet

`round(x + 0.4999)`

, so I'm going to put it down. Note that this works with any Python version. Changes made to the Python rounding scheme has made things difficult. See this post.Without importing, I use:

Why this worksFrom the docs

Therefore 2.5 gets rounded to 2 and 3.5 gets rounded to 4. If this was not the case then rounding up could be done by adding 0.5, but we want to avoid getting to the halfway point. So, if you add 0.4999 you will get close, but with enough margin to be rounded to what you would normally expect. Of course, this will fail if the

`x + 0.4999`

is equal to`[n].5000`

, but that is unlikely.## @blubberdiblub 2016-11-14 09:08:42

Using 0.4999, it will fail to give a correct result for any input in between ???.0000 and ???.0001 (open interval), not just exactly ???.0001. For instance, if you try it with 3.00005, you will get a result of 3 instead of the expected 4. Of course you can decrease the likelihood of this happening by adding more and more digits up to the maximum precision of floats, but what's the point to that if there are more robust and intuitive solutions at hand, like using

`math.ceil()`

?## @Klik 2016-11-14 22:23:45

@blubberdiblub In my answer I state

`Without importing I use:`

. I've also mentioned that it will fail if the`x + 0.4999`

is equal to`[n].5000`

.## @blubberdiblub 2016-11-17 05:59:30

Yes, you state in your answer that your solution is without importing, but I don't see the value of it. The

`math`

module and`math.ceil()`

is in the standard library, so available everywhere for all practical purposes without installing extra stuff. And regarding your mention of when it fails, this is incomplete in your answer, as it fails for a whole interval, not just for a single point. Technically, you could argue you are correct, as you sayifand notiff, but it will make the impression on the casual reader that it is less likely than it really is.## @user6612280 2016-07-20 07:07:54

You can use floor devision and add 1 to it. 2.3 // 2 + 1

## @guival 2017-02-24 10:34:58

or use

`ceil()`

instead of weirdly doing the opposite and then compensating## @Carl Thomé 2017-07-26 15:36:22

This won't work. For example:

`from math import ceil; assert 4 // 2 + 1 == ceil(4 / 2)`

## @Daniel 2016-06-01 02:46:14

I'm basically a beginner at Python, but if you're just trying to round up instead of down why not do:

## @Pranav Shukla 2016-06-01 02:52:38

This will not work for any integer i where 2.5 < integer < 3. The desired value after rounding up is 3 but your expression will turn it into 4.

## @Klik 2016-09-08 05:33:57

I think you mean

`round(integer + 0.5)`

This is what I often do## @David Bau 2016-02-01 08:23:29

If working with integers, one way of rounding up is to take advantage of the fact that

`//`

rounds down: Just do the division on the negative number, then negate the answer. No import, floating point, or conditional needed.For example:

## @Klik 2016-09-08 06:33:49

What about when you don't need to perform any math operation? I.e. you just have one number.

## @Marco smdm 2017-04-04 08:57:13

@Klik: then you can just divide by 1 ==> -( -num // 1) and you are getting your answer :-) Have a nice day! David Bau: very nice proposal!

## @mini totent 2017-07-20 18:23:48

I timed all the answers in here and this was five times faster than the next best (math.ceil). @Andreas had the same time

## @Salyangoz 2018-09-04 08:48:21

very nice solve without importing the math lib.

## @Nearoo 2019-01-06 18:44:06

@minitotent That is not surprising since it's simple integer division and a couple of single-cycle operations. This is the sort of answer that gets you a job: Understanding not only the language, but all the layers of abstractions beneath it.

## @NuclearPeon 2015-03-27 19:21:19

The syntax may not be as pythonic as one might like, but it is a powerful library.

https://docs.python.org/2/library/decimal.html

## @user3074620 2014-05-11 07:28:39

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.

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.

## @naught101 2015-08-19 13:04:36

Nice. You can also use

`//`

for integer division, so this becomes`21 // 5 + (21 % 5 > 0)`

.## @Nico Schlömer 2017-07-16 21:39:00

This is the best solution if only integers are involved. No unnecessary

`float`

s. Nice.## @Fenmaz 2015-01-14 08:15:01

I know this is from quite a while back, but I found a quite interesting answer, so here goes:

This fixes the edges cases and works for both positive and negative numbers, and doesn't require any function import

Cheers

## @Diblo Dk 2015-06-19 20:44:41

This will still rounds down

`-round(-x-0.3) = x`

## @Alexey 2014-07-30 19:27:18

Be shure rounded value should be float

but

## @gorttar 2014-06-15 00:34:53

To do it without any import:

## @Lisa 2013-04-26 16:27:43

You might also like numpy:

I'm not saying it's better than math, but if you were already using numpy for other purposes, you can keep your code consistent.

Anyway, just a detail I came across. I use numpy a lot and was surprised it didn't get mentioned, but of course the accepted answer works perfectly fine.

## @Marco smdm 2017-04-04 08:52:02

Using numpy is nice too. The easiest would be with math since it is already part of python built in libraries. It makes more sense. Instead as you mentioned if you use a lot numpy for other issues, then it makes sense and consistent to use numpy.ceil :-) Good hint!

## @AtilioA 2018-08-26 13:23:35

Great if you're already using numpy (my case).