154

I want to take a floating-point number and round it down to the nearest integer. However, if it's not a whole, I always want to round down the variable, regardless of how close it is to the next integer up. Is there a way to do this?

3
  • 2
    A possible difficulty is that IEEE floating point formats can represent numbers so large that the grandularity is larger than 1. So that, while you can round x down rounding x+1 down will not give you the result you expect. Jun 17, 2013 at 7:05
  • Please post some examples. Jun 17, 2013 at 7:06
  • "Round down" and "round to the nearest integer" are two different things.
    – Mr. Clear
    Mar 6, 2022 at 10:36

12 Answers 12

215
int(x)

Conversion to integer will truncate (towards 0.0), like math.trunc.
For non-negative numbers, this is downward.

If your number can be negative, this will round the magnitude downward, unlike math.floor which rounds towards -Infinity, making a lower value. (Less positive or more negative).

Python integers are arbitrary precision, so even very large floats can be represented as integers. (Unlike in other languages where this idiom could fail for floats larger than the largest value for an integer type.)

13
  • 10
    int(0.6) = 0 rather than 1
    – Helin Wang
    Nov 8, 2013 at 0:05
  • 54
    @HelinWang That's exactly what OP asked for. Nov 18, 2013 at 9:57
  • 6
    This seems like the most Pythonic approach.
    – Gyan Veda
    Jun 16, 2014 at 19:40
  • 31
    This works well for positive numbers, but negative numbers will be rounded up: int(-23.3) == 23
    – Alex Riley
    Nov 8, 2014 at 19:36
  • 3
    and does not work for number beyond the integer range such as 600851475143, it will basically flag a memory error. Jan 20, 2016 at 15:26
85

One of these should work:

import math
math.trunc(1.5)
> 1
math.trunc(-1.5)
> -1
math.floor(1.5)
> 1
math.floor(-1.5)
> -2
3
  • 15
    The output from math.trunc is an integer, while the output of math.floor is a float.
    – evedovelli
    Nov 29, 2013 at 15:33
  • 8
    @evedovelli: Not really anymore. type(math.floor(1.51)) -> int and type(math.trunc(1.51)) -> int as of python 3.6.0
    – SKPS
    May 3, 2017 at 10:23
  • 5
    These options are more explicit than "int(x)" and hence are more Pythonic.
    – Tristan
    Jan 7, 2019 at 16:53
55
x//1

The // operator returns the floor of the division. Since dividing by 1 doesn't change your number, this is equivalent to floor but no import is needed. Notes:

  1. This returns a float
  2. This rounds towards -∞
1
  • Nice addition. int(-1.1) == -1 while -1.1//1 == -2.0 however decimal.Decimal('-1.1')//1 == decimal.Decimal('-1') (as documented, claim 2 isn't true for decimal), so relying on how // behaves is not fully stable, even today.
    – Tino
    Jun 3, 2018 at 8:22
34

To get floating point result simply use:

round(x-0.5)

It works for negative numbers as well.

1
  • 2
    but it's wrong for already rounded numbers like 1: 1 - 0.5 = 0.5 and round(0.5) = 0, so 1 will be transformed to 0
    – inyutin
    Dec 21, 2020 at 22:20
31

I think you need a floor function :

math.floor(x)

2
  • 7
    in python 2 it returns a float while in python 3 it returns int Jun 17, 2013 at 7:06
  • 1
    int(math.floor(x)) or float(math.floor(x))
    – Jeff
    Jul 8, 2014 at 17:11
9

a lot of people say to use int(x), and this works ok for most cases, but there is a little problem. If OP's result is:

x = 1.9999999999999999

it will round to

x = 2

after the 16th 9 it will round. This is not a big deal if you are sure you will never come across such thing. But it's something to keep in mind.

4
  • 21
    That is because 1.9999999999999999 is actually equal to 2.0 in the internal float64 representation. I. e. it's already rounded as soon as it is parsed into a float, as a 64 bit float cannot represent that many significant digits. You can verify that with evaluating 1.9999999999999999 == 2.0. And if you suspect that the equals operation does some rounding on floats, you can compare the binary representation with struct.pack("d", 1.9999999999999999) == struct.pack("d", 2.0), which is also equal. Sep 6, 2015 at 3:26
  • 4
    And if that's exactly your point, then I don't see what's wrong with int(). The value is already 2.0 and it will convert it happily into 2. Sep 6, 2015 at 3:33
  • 1
    If OP's (or whomever reads this in the future) intention is to use the nearest integer ( and not the round-up value) for whatever reason, then it would be something to keep in mind.
    – lokilindo
    Aug 13, 2016 at 3:45
  • 4
    @lokilindo But this has nothing to do with int(), it solely has to do with an improper use of float, as 1.9999999999999999 is rounded up to 2.0 at compile time (while int() is called on execution time). If you use the right data type for the variable, everything works as expected: int(decimal.Decimal('1.9999999999999999999999999999999999999999999999999999999')) gives 1
    – Tino
    Jun 3, 2018 at 8:04
6

If you don't want to import math, you could use:

int(round(x))

Here's a piece of documentation:

>>> help(round)
Help on built-in function round in module __builtin__:

round(...)
    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.
2
  • Thanks for your answer. Next time you'll get a better reception if you write proper code (close parenthesis), and give some documentation.
    – Geoff
    May 29, 2014 at 22:20
  • 2
    round was already discussed and rejected as an answer when this question was asked a year ago. OP wants math.floor.
    – Adam Smith
    May 29, 2014 at 22:25
3

If you working with numpy, you can use the following solution which also works with negative numbers (it's also working on arrays)

import numpy as np
def round_down(num):
    if num < 0:
        return -np.ceil(abs(num))
    else:
        return np.int32(num)
round_down = np.vectorize(round_down)

round_down([-1.1, -1.5, -1.6, 0, 1.1, 1.5, 1.6])
> array([-2., -2., -2.,  0.,  1.,  1.,  1.])

I think it will also work if you just use the math module instead of numpy module.

3

Just make round(x-0.5) this will always return the next rounded down Integer value of your Float. You can also easily round up by do round(x+0.5)

1

Don't know if you solved this, but I just stumble upon this question. If you want to get rid of decimal points, you could use int(x) and it will eliminate all decimal digits. Theres no need to use round(x).

-2

It may be very simple, but couldn't you just round it up then minus 1? For example:

number=1.5
round(number)-1
> 1
4
  • 4
    This gives the wrong answer for whole integers. For instance, 2.0 rounded up is 2, and if you subtract 1 you get the incorrect result 1. May 29, 2014 at 22:45
  • @PascalCuoq I don't understand your problem. Do you want 1.0 as the result? Because OP clearly wanted to round then number off to the nearest integer. May 14, 2015 at 9:43
  • 1
    @bad_keypoints I don't think that the OP wants to round 2.0 to 1. May 14, 2015 at 14:46
  • @PascalCuoq sorry, I just looked back at the answer in comment thread of which we are. May 15, 2015 at 8:28
-7

I used this code where you subtract 0.5 from the number and when you round it, it is the original number rounded down.

round(a-0.5)

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.