-1

how to round down and round up the decimal vlaues in pytho.

user input

      a =2.336  After round up it should be like a =2.34
      a =4.13623  After round up it should be like a =4.14

Is there any built in functions in python to manipulate

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Wilson
  • 11
  • 4
  • 1
    Maybe you're just looking for `round(a, 2)` to round to 2 digits? Your question is not quite clear, are you looking for ceil/floor to a given precision, or just the general round operation to a give precision? – joanis Mar 29 '22 at 18:23
  • If you're just looking for rounding, see https://stackoverflow.com/q/13479163/3216427 Otherwise the answer below are good. – joanis Mar 29 '22 at 18:26
  • 1
    Does this answer your question? [Round float to x decimals?](https://stackoverflow.com/questions/13479163/round-float-to-x-decimals) – thesecretmaster Mar 30 '22 at 11:28

2 Answers2

2

you could try something like

a = 2.336
a = math.ceil(a*pow(10,2))/pow(10,2)

here 2 in pow(10,2) stands for the precision you need behind the decimal point

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
1

You can use the ceil() and floor() functions from the math module. Here's some code to help you get started.

def round_up(n, decimals=0):
   multiplier = 10 ** decimals
   return math.ceil(n * multiplier) / multiplier

Try figuring out how the decimal and multiplier variables are working here.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
amur
  • 67
  • 8