Round function in python

Python round() Function

The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.

The default number of decimals is 0, meaning that the function will return the nearest integer.

Syntax

Parameter Values

Parameter Description
number Required. The number to be rounded
digits Optional. The number of decimals to use when rounding the number. Default is 0

More Examples

Example

Round to the nearest integer:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Python Round to Int – How to Round Up or Round Down to the Nearest Whole Number

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Python Round to Int – How to Round Up or Round Down to the Nearest Whole Number

When working with float values (numbers with decimal values) in our Python program, we might want to round them up or down, or to the nearest whole number.

In this article, we’ll see some built-in functionalities that let us round numbers in Python. And we’ll see how to use them with some examples.

We’ll start with the round() function. By default, it rounds a number to the nearest whole number. We’ll also see how to use the function’s parameters to change the type of result returned to us.

We’ll then talk about the math.ceil() and math.floor() methods which rounds up and rounds down a number to the nearest whole number/integer respectively. These two methods are from the built-in math module in Python.

How to Use the round() Function to Round to the Nearest Whole Number

The round() function takes in two parameters. Here’s what the syntax looks like:

round(number, decimal_digits)

The first parameter – number – is the number we are rounding to the nearest whole number.

Читайте также:  Java list метод set

The second parameter – decimal_digits – is the number of decimals to be returned. The default value is 0.

In our first example, we’re using only one parameter – the number to be rounded, which is 2.56789 .

When we passed in the number variable to the round() function, it got rounded to the nearest whole number which is 3.

That’s how easy it is to use!

Now, let’s work with the second parameter.

x = 2.56789 print(round(x, 2)) # 2.57

The code above is similar to the last example except for the second parameter. We passed in a value of two. This will round the number to the nearest hundredth (two decimal places).

In our case, 2.57 was returned. That is, 2.56789 to 2.57.

Let’s see one last example to fully understand how the second parameter works.

x = 2.56789 print(round(x, 3)) # 2.568 

Now, we’ve made the second parameter 3. We’ll get the number rounded to the nearest thousandth (three decimal places).

The initial number – 2.56789 – was rounded to 2.568.

How to Use the math.ceil() Method to Round Up a Number to the Nearest Whole Number

The math.ceil() method simple takes in the number to be rounded up as its parameter. Here’s what the syntax looks like:

import math x = 5.57468465 print(math.ceil(x)) # 6 

In the code above, you’ll notice that we first imported the math module: import math . This give us access to all the methods provided by the module.

We created an x variable which has 5.57468465 as its value.

In order to round this number up to the nearest whole number, we passed in the number (in the x variable) to the math.ceil() method: math.ceil(x) .

The resulting value from this operation, as can be seen in the code above, is 6.

How to Use the math.floor() Method to Round Down a Number to the Nearest Whole Number

Just like we did in the last section, in order to use the math.floor() method, we must first import the math module.

Here’s the syntax for math.floor() method:

import math x = 5.57468465 print(math.floor(x)) # 5

As expected, we passed in the number to be rounded down to the math.floor() method: math.floor(x) . The x variable has the number 5.57468465 stored in it.

This number got rounded down to 5.

Conclusion

In this article, we talked about three built-in functionalities in Python that let us round numbers.

The round() function rounds a number to the nearest whole number.

The math.ceil() method rounds a number up to the nearest whole number while the math.floor() method rounds a number down to the nearest whole number. These two methods are both accessible through the math module.

With the examples given in each section, we were able to see how to use each functionality to obtain our desired result.

Читайте также:  Найти минимум массива java

Источник

The round() Function in Python

Python supports object-oriented programming and has concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. An integral part of Python are its built-in functions.

We’ve written a series of articles to help you learn and brush up on the most useful Python functions. In this article, we’ll learn about the round() function in Python and how to use it.

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Python Exit commands, and Type and Isinstance In Python for more content on Python coding interview preparation.

Having trained over 10,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll cover:

  • What Is the round() Function in Python and What Does It Do?
  • The round() Function in Python: Syntax
  • The round() Function in Python: Example
  • FAQs on the round() Function in Python

What Is the round() Function in Python and What Does It Do?

The round function in Python rounds a number to a specific decimal place and returns a floating-point number rounded as per our specifications. It takes the following arguments:

The number of decimal places is set to zero by default. So the round() function returns the nearest integer to the number passed if the number is the only argument passed.

The round() Function in Python: Syntax

 round(number, numberOfDigitsPostDecimal) 
  • number refers to the number to be rounded off.
  • numberOfDigitsPostDecimal refers to the number of digits after the decimal number will be rounded off to. If you don’t provide this argument, you’ll get an integer as the result of round() applied on any float type number.

The round() Function in Python: Example

Here, we take a look at how you can use the Python function round() next time you need it:

Code

 # Rounding off integers using round() print("Round integer 33: ") print(round(33)) # Rounding off floating point, to show that at 0.5, it rounds to the closest even number print("Round odd and even, positive float 33.5 and 34.5: ") print(round(33.5)) print(round(34.5)) print("Round odd and even, negative float -33.5 and -34.5: ") print(round(-33.5)) print(round(-34.5)) # Rounding off floating point, case of less than, equal to, and more that 0.5 being rounded off print("Round positive float 0.4, 0.5, 0.6: ") print(round(0.4)) print(round(0.5)) print(round(0.6)) # Rounding off floating point, case of negative numbers, less than, equal to, and more that -0.5 being rounded off print("Round negative float -0.4, -0.5, -0.6: ") print(round(-0.4)) print(round(-0.5)) print(round(-0.6)) # Rounding off floating point 0.5, upto various places of decimal print("Round 0.6 upto 0,1, and 2 places of decimal: ") print(round(0.6,0)) print(round(0.6,1)) print(round(0.6,2)) 

Output

 Round integer 33: 33 Round odd and even, positive float 33.5 and 34.5: 34 34 Round odd and even, negative float -33.5 and -34.5: -34 -34 Round positive float 0.4, 0.5, 0.6: 0 0 1 Round negative float -0.4, -0.5, -0.6: 0 0 -1 Round 0.4 upto 0, 1, and 2 places of decimal: 0.0 0.4 0.4 Round 0.5 upto 0, 1, and 2 places of decimal: 0.0 0.5 0.5 Round 0.6 upto 0, 1, and 2 places of decimal: 1.0 0.6 0.6 


Note that in the case of 0.5, the round() function rounds the number off to the nearest even number.

Читайте также:  Параметры post get php

Found this article helpful? You can learn about more Python functions in our learn folder.

FAQs on the round() Function in Python

Q1. What does round() in Python return?

The round function in Python takes any number as an argument and returns a floating-point number rounded as per our optionally provided specifications. If no specifications are provided, it rounds to zero decimal places, giving us the nearest integer.

Q2. How do you round a float in Python 3?

You round off a float number to your desired decimal place using the built-in function round() in Python.

Q3. How do you round a number num to three decimal places in Python?

The syntax for that would look like this: round(num, 3)

Q4. In Python, the round() function rounds up or down?

The round() function can round the values up and down both depending on the situation. For 0.5, it rounds up. For =0.5, the round() function rounds the number off to the nearest even number. So, 0.5 is rounded to zero, and so is -0.5; 33.5 and 34.5 are both rounded off to 34; -33.5 -34.5 are both rounded off to -34, and so on.

Q5. By default, round() in Python rounds to how many decimal places?

By default, if not mentioned, the round() method rounds the given number to zero decimal places.

Ready to Nail Your Next Coding Interview?

Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Источник

Оцените статью