In this lesson we will learn about Python round method for numbers. This method will return a number rounded to a specific digit after the decimal point.
Python round method
Python round method will return the rounded value of a number (specifically a float value ) to the specified digits ( n
) from the decimal point. This method takes two arguments. The first argument will be the float value which needs to be rounded and the second argument will be digit limitation from decimal point.
Syntax
Here is the syntax for Python round method:
round( a [, n] )
- a – This is the value which needs to be rounded
- n – This says how many digits will be showed after decimal points.
The return value will be an integer if it is called with one argument, otherwise it will be the same type as the number.
Example
Here are some examples of Python round method:
print "round(0.56247, 2) : ", round(0.56247, 2) print "round(-0.8541, 2) : ", round(-0.8541, 2) print "round(100.045, 1) : ", round(100.045, 1) print "round(-99.068, 2) : ", round(-99.068, 2)
When we run above program, it produces following result:
round(0.56247, 2) : 0.56 round(-0.8541, 2) : -0.85 round(100.045, 1) : 100.0 round(-99.068, 2) : -99.07
The post Python round method for numbers appeared first on JS Tricks.