Rounding down in Python

Asked

Viewed 6,692 times

3

I’m writing a program in Python and I came up with a question here. Before coming to ask I searched for "Python Rounding" here on Sopt and found two old questions with the subject, but both did not answer my question.

That question here How to "round" a float in Python? talks about the function round that I already know, but she does not answer me, because the function round works always rounding to the nearest number, let’s see the examples:

a = 5.92
b = round(a,0)
print(b)

The result shown will be 6.

a = 5.22
b = round(a,0)
print(b)

The result shown will be 5. That is, it only rounds down if the decimal fraction was below 0.5.

I also found this question How to round up with Python?, that addresses the function ceil, that makes rounding upwards. As I wish to round down, this, of course, also does not suit me, because let’s look at the examples:

from math import ceil
a = 5.92
b = ceil(a)
print(b)

The result shown will be 6.

from math import ceil
a = 5.22
b = ceil(a)
print(b)

The result shown will also be 6.

The question is: Which function rounds down in Python?

3 answers

9


The contrary function of the ceil() (ceiling) is the function floor() (floor). See the documentation (it is on the same page of ceil().

from math import floor
a = 5.92
b = floor(a)
print(b)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

In some cases the person may want to use the trunc(). In general conceptual people wrong the rounding, and forget the negatives. With a positive number the two result equal, but in negative numbers gives different results.

  • It worked here, thank you!

2

Tbm can be done manually, it is very simple, even using the round, with the condition, it will always take the whole part of the value.

numero = float(input('Digite um numero real: '))
num = round(numero)
if numero - num < 0:
    num -= 1
print(num)

0

What you’re trying to figure out is chão of a number.

The chão of a number real (floating point number) is the maior integer menor that that paragraph real.

Example 01:

The chão of 22.6 is 22

Yeah, the maior integer inferior the actual value number 22.6 results in the value of 22

Example 02:

The chão of -22.6 is -23

Yeah, the maior integer inferior the actual value number -22.6 results in the value of -23.

Before questioning the result shown in this 2nd example you should note the direção in the layout of the values on the straight line real.

On the real line the numbers are distributed in the direction of esquerda to the direita. In other words, given a real number N, every number maior that N will be placed at your direita...

22.6, 23.0, 27.0 29.6, 30.4...

...every number menor that N will be placed at your esquerda...

...16.0, 17.5, 20.0, 22.4, 22.6, 23.0

Faced with this explanation we can now clearly see the result of 2º exemplo, that is to say,

-25.0, -24.7, -23.0, -22.6, -22.0,...

That is, as the value -23 is positioned at the esquerda of -22.6. this means that the chão of -22.6 is the number -23.

To calculate the chão of a number in Python it is necessary to execute the method floor class math.

To show the operation of this method I developed the following algorithm...

from math import floor

lista = [-22.6, 15.3, 16.3, 17.9, 21.4, 22.5, 25.6, 26.9]

resultado = list()
for numero in lista:
    # Calculando os valores dos Tetos de cada valor contido em "lista".
    resultado.append(floor(numero))

print(f'\033[32mOs valores dos Tetos são: {resultado}\033[m')

Check the functioning of the algorithm on repl it..

Note that this algorithm calculates the chão of all elements of the previous list.

Also note that the result of the method floor will always be a value inteiro.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.