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

4 Surprising Ways Artificial Intelligence will Empower Consumers

Ever wonder what makes Siri search the items inside the iPhone and on the web by recognizing your voice? How does Google listening work? Artificial Intelligence and data science have already infused in this consumer-oriented generation, but we have not realized it yet. It has been quite a while that natural language processing, speech recognition, and gesture recognition system took over the market with their all new features, but what are the new things in AI that are yet to come? Few amazing transformations are: Retailing sector to become AI-ready Voice Recognition will change the game Search engines to become smarter Machine learning to bring revolutionary changes Recently we got to learn about a verbal spat between Elon Musk and Mark Zuckerberg over the probable risk and opportunities brought by AI . Believe it or not, but we already accustomed to Artificial Intelligence and probably waiting to witness jaw-dropping inventions. Some of AI inventions we have known till yet are: Mach

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 arrays in Python

Arrays in Python give you a huge amount of flexibility for storing, organizing, and accessing data. This is crucial, not least because of Python’s popularity for use in data science. But what precisely is an array? And how do you use arrays in Python? Also read: How to use dictionaries in Python Read on, and we’ll shed some light on the matter. What is an array? An array is a way to store multiple values in a single variable. That means that you can use a single “reference” in order to access your data. A list is also an example of a variable that stores multiple values, but has some slight differences. When using lists in Python, you store a series of values each with a numbered index. For example, this is how you would create a list of fruits in Python: fruits = [“apple”, “orange”, “pear”, “nectarine”] If we then say: print(fruits[3]) We will see “nectarine” appear on the screen (the first entry is stored as “0”). Also read: How to use lists in Python This is not an