{"id":24570,"date":"2021-10-22T08:57:49","date_gmt":"2021-10-22T03:27:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=24570"},"modified":"2021-11-05T19:39:07","modified_gmt":"2021-11-05T14:09:07","slug":"python-round-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-round-function-with-examples\/","title":{"rendered":"Python round() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for reversed() Function<\/a> The round() function returns a floating-point number with the specified number of decimals that is a rounded version of the specified number.<\/p>\n The function will return the nearest integer if the number of decimals is set to 0.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n number:<\/strong> This is required. It is the number that should be rounded.<\/p>\n digits:<\/strong> This is Optional. When rounding a number, the number of decimals to use(up to how many digits to be rounded after decimal) The default value is 0.<\/p>\n Return Value:<\/strong><\/p>\n The function round() returns the<\/p>\n If digits<\/strong>\u00a0is not provided, the nearest integer to the given number is used; otherwise, the number is rounded off to digits.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for reversed() Function round() Function in Python: The round() function returns a floating-point number with the specified number of decimals that is a rounded version of the specified number. The function will return the nearest integer if the number of decimals is set to 0. Syntax: …<\/p>\n
\nround() Function in Python:<\/strong><\/p>\nround(number, digits)<\/pre>\n
Given number = 10.5678\r\nGiven no of digits = 2<\/pre>\n
The rounded value of the given number 10.5678 upto 2 digits = 10.57<\/pre>\n
Given number = 7.8<\/pre>\n
The rounded value of the given number 7.8 = 8<\/pre>\n
Program for round() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 10.5678\r\n# Give the digits (up to how many digits to be rounded after the decimal)as\r\n# static input and store it in another variable.\r\ngvn_digitss = 2\r\n# Get the rounded value of the given number by passing the given number\r\n# and given digits as arguments to the round() function.\r\n# Store it in another variable.\r\nnumbraftr_round = round(gvn_numb, gvn_digitss)\r\n# Print the rounded value of the given number.\r\nprint(\"The rounded value of the given number\", gvn_numb,\r\n \"upto\", gvn_digitss, \"digits = \", numbraftr_round)\r\n<\/pre>\n
The rounded value of the given number 10.5678 upto 2 digits = 10.57<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the number as user input using the float(input()) function and store it in a variable.\r\ngvn_numb = float(input(\"Enter some random number = \"))\r\n# Get the rounded value of the given number by passing the given number\r\n# as an argument to the round() function.\r\n# Store it in another variable.\r\nnumbraftr_round = round(gvn_numb)\r\n# Print the rounded value of the given number.\r\nprint(\"The rounded value of the given number\", gvn_numb, \"= \", numbraftr_round)\r\n<\/pre>\n
Enter some random number = 7.8\r\nThe rounded value of the given number 7.8 = 8<\/pre>\n
\n