Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Rounding Numbers in Python (Summary)

Whew! What a journey this has been!

In this video course, you’ve learned that:

  • There are various rounding strategies, which you now know how to implement in pure Python.
  • Every rounding strategy inherently introduces a rounding bias, and the rounding half to even strategy mitigates this bias well, most of the time.
  • The way in which computers store floating-point numbers in memory naturally introduces a subtle rounding error, but you learned how to work around this with the decimal module in Python’s standard library.
  • You can round NumPy arrays and pandas Series and DataFrame objects.
  • There are best practices for rounding with real-world data.
Download

Course Slides (.pdf)

1.1 MB
Download

Sample Code (.zip)

3.7 KB

00:00 In the previous lesson, I showed you rounding in other Python numeric libraries. In this lesson, I’ll summarize the course and point you at other sources of info.

00:10 This course was all about rounding numbers. In it, you learned many different algorithms for rounding, including truncate, rounding up, rounding down, rounding half up, rounding half down, rounding half away from zero, and rounding half away from even. Python’s built-in round() function uses the half away from even algorithm, which is the default mechanism in the IEEE floating-point standard.

00:37 When you round lots of numbers in your data, you might introduce bias. This could result in changing the shape of the data from the original.

00:48 Of the algorithms covered in this course, The half away from even was the least biased, and it really is only problematic if you’ve got a set of numbers that is predominantly odd or even. Bias isn’t the only problem rounding can introduce.

01:03 It can also trigger edge conditions in floating-point numbers.

01:07 Floating point is capable of storing a mind-boggling wide range of values, but it isn’t precise and not every number can be stored exactly. This can cause weird behavior when you round. Values that you thought should round down, might round up because of this lack of precision.

01:24 If you’re looking for precise numeric representation, Python’s decimal class in the Decimal module gives you just that. It even supports a variety of rounding algorithms.

01:34 The best practice when using data that will be rounded is to store the data with as much precision as you can and leave the rounding to the latest stage in your calculations as possible.

01:46 In this course, you’ve seen integers, floats, and the decimal class. Python can also store fractions. For a complete breakdown of Python’s number storing capabilities see this tutorial. In the second lesson, I used a random value to emulate price changes on the stock market.

02:03 If you want to learn more about random numbers in Python, you can check this out.

02:08 I very briefly hinted at how the round() function actually calls a special method on the numeric classes that it is rounding. If you’d like to learn more about these kinds of special methods, this course explains some of them.

02:20 This is actually part three of a three-part course that does a deep dive on object-oriented programming. If you’re not familiar with classes and inheritance, start at the beginning.

02:29 If you just want to learn more about the special methods, also known as dunder methods, then you can start with this one, which is part three.

02:36 And finally, if you want to learn more about how floating point gets stored in your computer, along with all sorts of other bits and bytes, this course talks about the binary representations that your computer uses and how to play with them in Python.

02:51 That’s all for this course. Thanks a million to the power of 51. That’s magnitude 306, which you can still fit inside of a float. They’re huge.

Become a Member to join the conversation.