How to round decimal numbers to the largest integer? python

Asked

Viewed 264 times

1

I’m in need of arredondar values decimais for the highest integer value mais próximo of the relevant value.

For example, I have the value...

22.6

...and would like to receive as a result the value...

23 

How can I perform this operation in Python?.

The algorithm in which I wish to implement this operation is listed below.

for i in range(len(lista)):
    if lista[i][0] == esp:
        contador += lista[i][2]/lista[i][1]
        ocorrencias += 1
print(pca*(contador/ocorrencias))
esp, pca = input().split()

I do some operations with a list and always get a value close to the correct one, with a few decimal digits below

  • Duplicate of https://answall.com/q/317381/5878

2 answers

2

Import the library math, and in it there are two functions, the floor() and the ceil(), to floor() rounds down and the ceil() rounded up to the nearest integer

math.floor(30.4)
(30.0)

math.ceil(30.4)
(31.0)

Edit: More Examples:

math.floor(99.1)
(99.0)

math.ceil(99.1)
(100.0)

math.floor(22.4)
(22.0)

math.ceil(22.4)
(23.0)
  • I want 2 more examples. + 1

  • 1

    done, added to Edit

0

What you’re trying to figure out is, nothing more, nothing less than the teto of a number.

The teto of a number real (which may be a floating point number) is the menor number inteiro greater than that actual value.

Example 01:

The teto of 22.6 is 23

For the menor integer superior the actual value number 22.6 has as a result the number 23

Example 02:

The teto of -22.6 is -22

For the menor integer superior the actual value number -22.6 results in the number `-22.

Before making any decision before this 2nd example you should note the direção the provision of values in real straight.

In the reta real the numbers are distributed in the sentido 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, 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

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

-25.0, -24.7, -22.6, -22.0, -21.5, ...

That is, as the value -22 is positioned to the right of -22.6. this means that the teto of -22.6 is the number -22.

Now, after all this explanation, how do we calculate the teto of a number with the Python?

To calculate the teto of a number in Python it is necessary to execute the method ceil class math.

To show how this método I developed the following algorithm...

from math import ceil

lista = [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(ceil(numero))

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

See how the algorithm works on repl it.

Note that this algorithm calculates and displays the value of teto of all values in the previous list.

Also note that the function result ceil will always be a value inteiro.

Browser other questions tagged

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