How to Round number in Python
Python provides the built-in round() function, which used to round off a number to a given number of digits. It takes the two arguments, first is n, second is n digits and then it returns number n after rounding it to ndigits. By default, it rounds off the number n to the nearest integer.
For example - If we want to round off a number, let's suppose 7.5. It will be rounded off to the nearest whole number is 7. However, the number 7.56 will be rounded off to 7.5 by one places to give.
The round() function is essential when working with the number of floats that may have many decimal places. The round() function makes easy and simple. The syntax is given below.
The parameters are -
Let's understand the following example -
15 26 25
Now, the second parameter is used.
25.47 25.428 25.42
The real-life example of the round() function
The round() function is most useful while changing fractions to decimals. We generally get the number of a decimal points such as if we do 1/3 then we get 0.333333334, but we use either two or three digits to the right of the decimal points. Let's understand the following example.
6 5 6
The round() function rounds 5.5 up to 6 and 6.5 down to 6. This is not a bug, the round() behaves like this way.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week