Skip to main content

How to round in Python

Working on Python Project

Credit: Adam Sinicki / Android Authority

Want to know how to round in Python? This can be useful for a wide range of applications, particularly when converting a float into an integer, or when presenting data in a readable manner.

Thankfully, this is an easy job: there are actually three separate ways to round in Python.

How to round in Python: know your options

First, you can use the built-in round() function. This will round values either up or down. math.floor(), meanwhile, will round the value down. Finally, math.ceil() will round the value up to the nearest whole.

In order to use the latter two statements, you will need to import the math module with the line: import math.

To see how this might work, use the following code:

import math

number = 7.234

print(round(number))

print(math.floor(number))

print(math.ceil(number))

Now hit run, and you will see the output looks like so:

7

7

8

As you can see, the first two options rounded down to 7, while the final option rounded up to 8. Remember: when using round(), the value will round up whenever the value is higher than .5.

Other tricks using the math module

Now you know how to round in Python, there plenty of other neat things you can do with the math module. Some examples include:

  • squrt() – Gets the square root of a number
  • Math.sin() – Returns the sine of a number
  • tan() – Returns the tangent of the number
  • pi – Gives you pi

Learn more

If you want to learn more about coding in Python, a great place to start is with our introductory post that will walk you through the set up process and a basic first project.

If you want to take this knowledge further though, then you should check out our list of the best online Python courses. Any of these will provide you with a full and detailed introduction to Python that can put you on the right path to a development career. Or if you are really new and looking for the best introductory option, we highly recommend Coding with Python: Training for Aspiring Developers, which you can nab for just $49.99, which is an absolute steal as the course is valued around $700.

$49 .99
Coding with Python: Training for Aspiring Developers Bundle
Save $641 .01
Buy it Now
Coding with Python: Training for Aspiring Developers Bundle Buy it Now
Save $641 .01 $49 .99


source https://www.androidauthority.com/how-to-round-in-python-1134475/

Comments

Popular posts from this blog

How to unhide or show folders in mx player list

In this blog post, I tell you about how to Show or Hide folders from MX player list. There are two methods to Show folder from MX Player list. Method 1: Unhide / show folders If you want to temporarily Show / Unhide hidden folder from MX Player list, then go to Settings and untick Recognize .nomedia ".  Method 2: Permanently unhide / show folder: Open memory by any file explorer and I recommend X-Plore, and open the folder that is Hidden and find the file " .nomedia ". If you didn't find it, you should first enable "Show files hidden files that starts with .(dot)". Delete the file and you just need to refresh MX Player list to take changes. Note: MX Player always hide those folders which file " .nomedia " exists.

How to use a web API from your Android app

Credit: Adam Sinicki / Android Authority A Web API is an online “application programming interface” that allows developers to interact with external services. These are the commands that the developer of the service has determined will be used to access certain features of their program. It is referred to as an interface because a good API should have commands that make it intuitive to interact with. An example of this might be if we want to get information about a user from their social media account. That social media platform would likely have a web API for developers to use in order to request that data. Other commonly used APIs handle things like advertising ( AdMob ), machine learning ( ML Kit ), and cloud storage. It’s easy to see how interacting with these types of services could extend the functionality of an app. In fact, the vast majority of successful apps on the Play Store will use at least one web API! In this post, we’ll explore how to use a web API from withi...

How to use AWS – a simple guide for beginners

Amazon Web Services (AWS) is Amazon’s powerful, market-leading solution for cloud computing. The platform offers a suite of products for businesses: security, cloud backup, machine learning, IoT solutions, and more. In this post, we will explore how to use AWS. What you need to know Many entrepreneurs and small businesses may assume that AWS is not for them. Perhaps the pricing will be too prohibitive, or it will require too much technical know-how. While both of these issues certainly do crop up from time-to-time, the truth is that Amazon’s offerings are extremely wide-reaching and include options at many different price points and levels of complexity. That is to say, that while some products might be off-limits, others are not. Some AWS products are completely free and very simple to get to grips with! See also: AWS vs Azure vs Google Cloud – Which certification is best for professionals? AWS includes over 175 different products, some of which don’t even require an AWS accou...