Home > Net >  How to roundup and round down the decimal part in python
How to roundup and round down the decimal part in python

Time:03-30

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

CodePudding user response:

you could try something like a = 2.336 a = math.ceil(a*pow(10,2))/pow(10,2)

here 2 stands for the precision you need behind the floating point

CodePudding user response:

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.

  • Related