Home python rounding number to the nearest whole

rounding number to the nearest whole




According to the rules of the number, they are rounded to the nearest integer, and if the fractional part of the number is 0.5 , the number is rounded up.

decided in this way, received 100/100 , but I would like to get rid of the number 1000000 , since when entering the number X, 4999999 rounds Up:

from math import *
X = Float (Input ())
a = Floor (x)
b = x - a
if (int (Round (B, 1000000) * 100)) & lt; 50:
  Print (int (x))
ELIF (INT (Round (B, 1000000) * 100)) == 50 OR (INT (ROUND (B, 2) * 100)) & gt; 50:
  Print (Ceil (X))

Answer 1, Authority 100%

If you need to round the numbers in a specific side, then use the following functions

And if you need when a certain residue rounded the number in one direction or another, you can try to use the balance of the division (%) of the two numbers and compare already on it

math.ceil () – rounding numbers to the biggest

math.floor () – rounding numbers to the smaller side

math.trunc () – discard fractional part

Answer 2


def proper_round (num, dec = 0):
  Num = STR (NUM) [: STR (NUM) .INDEX ('.') + Dec + 2]
  IF num [-1] & gt; = '5':
    RETURN FLOAT (NUM [: - 2- (NOT DEC)] + STR (int (NUM [-2- (NOT DEC)) + 1))
  RETURN FLOAT (NUM [: - 1])

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions