Round a floating-point number down to the nearest integer?
385,892
Solution 1
Simple
int(x)
will work as well.
Solution 2
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
Solution 3
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:
- This returns a float
- This rounds towards -∞
Solution 4
To get floating point result simply use:
round(x-0.5)
It works for negative numbers as well.
Solution 5
I think you need a floor function :
Author by
Anthony Perez
Updated on February 26, 2021Comments
-
Anthony Perez about 3 years
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?
-
dmckee --- ex-moderator kitten almost 11 yearsA 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.
-
Ashwini Chaudhary almost 11 yearsPlease post some examples.
-
Mr. Clear about 2 years"Round down" and "round to the nearest integer" are two different things.
-
-
voidMainReturn almost 11 yearsin python 2 it returns a float while in python 3 it returns int
-
Helin Wang over 10 yearsint(0.6) = 0 rather than 1
-
Petr Peller over 10 years@HelinWang That's exactly what OP asked for.
-
evedovelli over 10 yearsThe output from
math.trunc
is an integer, while the output ofmath.floor
is a float. -
Geoff almost 10 yearsThanks for your answer. Next time you'll get a better reception if you write proper code (close parenthesis), and give some documentation.
-
Adam Smith almost 10 years
round
was already discussed and rejected as an answer when this question was asked a year ago. OP wantsmath.floor
. -
Pascal Cuoq almost 10 yearsThis 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.
-
Gyan Veda almost 10 yearsThis seems like the most Pythonic approach.
-
Jeff almost 10 yearsint(math.floor(x)) or float(math.floor(x))
-
Alex Riley over 9 yearsThis works well for positive numbers, but negative numbers will be rounded up:
int(-23.3) == 23
-
bad_keypoints almost 9 years@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
. -
Pascal Cuoq almost 9 years@bad_keypoints I don't think that the OP wants to round 2.0 to 1.
-
bad_keypoints almost 9 years@PascalCuoq sorry, I just looked back at the answer in comment thread of which we are.
-
blubberdiblub over 8 yearsThat is because
1.9999999999999999
is actually equal to2.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 evaluating1.9999999999999999 == 2.0
. And if you suspect that the equals operation does some rounding on floats, you can compare the binary representation withstruct.pack("d", 1.9999999999999999) == struct.pack("d", 2.0)
, which is also equal. -
blubberdiblub over 8 yearsAnd 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. -
Muyide Ibukun over 8 yearsand does not work for number beyond the integer range such as 600851475143, it will basically flag a memory error.
-
lokilindo over 7 yearsIf 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.
-
SKPS almost 7 years@evedovelli: Not really anymore.
type(math.floor(1.51)) -> int
andtype(math.trunc(1.51)) -> int
as ofpython 3.6.0
-
Alon almost 7 yearsextremely sophisticated that is
-
Antony Hatchkins about 6 years@MuyideIbukun python? beyond the integer range?
-
Tino almost 6 years@lokilindo But this has nothing to do with
int()
, it solely has to do with an improper use offloat
, as1.9999999999999999
is rounded up to2.0
at compile time (whileint()
is called on execution time). If you use the right data type for the variable, everything works as expected:int(decimal.Decimal('1.9999999999999999999999999999999999999999999999999999999'))
gives1
-
Tino almost 6 yearsNice addition.
int(-1.1) == -1
while-1.1//1 == -2.0
howeverdecimal.Decimal('-1.1')//1 == decimal.Decimal('-1')
(as documented, claim 2 isn't true fordecimal
), so relying on how//
behaves is not fully stable, even today. -
Tino almost 6 years@MuyideIbukun
int(-1E99)
gives (at my side)-999999999999999967336168804116691273849533185806555472917961779471295845921727862608739868455469056L
which looks pretty ok to me. -
Tino almost 6 years@AlexRiley While you are mathematically correct, usually the "nearest lower integer" a human wants is towards 0 and not towards -inf. If your account balance is -23.3M$ you will rather think of having -23M than having -24M, right? Same is true if you have -23.9M; You will still pretend it's only -23M as long as you can ;)
-
xskxzr over 5 yearsWhat's the difference from this existed answer?
-
Tristan over 5 yearsThese options are more explicit than "int(x)" and hence are more Pythonic.
-
HosseyNJF over 4 years@Tino But if you had $23.1M, you would definitely want to round it up to $24; so I don't think your example applies here:)
-
Mikhail Gerasimov over 4 yearsI like this solution much less than
math.floor
: one should know howint
function works to see rounding down happens. The solution also relies on a detail of implementation ofint
function (even though it's unlikely to change).math.floor
on the other hand is explicit: just looking at it is enough to be sure rounding down will happen. -
AlGiorgio about 4 yearsAs Mikhail said in accordance with Python Zen: Explicit is better than implicit. That's it about using int for rounding values.
-
inyutin over 3 yearsbut 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