# Understanding the Round Function

**00:00**
In the previous lesson, I gave an overview of the course. In this lesson, I’ll show you Python’s built-in `round()`

function and why you should use it.

**00:08**
The algorithm you were likely taught in grade school for rounding goes something like this. Pick the position to the right of where you want to round. If the value of that digit is less than five, then throw away that digit and everything to the right of it.

**00:24**
Whereas if the value of that digit is five or more, increase the value of the digit to the left and chop everything else. But this isn’t a math course, it’s a Python course.

**00:37**
Let’s go use Python’s built-in `round()`

function in the

**00:42**
REPL. To round a number using the built-in function, you simply call the `round()`

function. Like before, 1.4 rounds to one and 1.6 rounds to two, as does 1.5 and wait a second. That’s a little unexpected.

**01:03**
It isn’t a bug, it’s a feature. I promise.

**01:08**
There is more than one algorithm to round numbers. And as with all sorts of choices in life, there are pros and cons for each of these algorithms. The built-in `round()`

function uses what is called the half-to-even rounding strategy, which is the default rounding rule in the IEEE standard that specifies how floating-point numbers work.

**01:29**
This course is all about the different kinds of rounding strategies and their consequences, but before going there, let’s briefly talk about why you might want to round at all. Throughout this course, I’m going to be using a utility library I’ve written with a variety of rounding methods in it.

**01:47**
The file it’s in is called `rounding.py`

, and here I’m showing the first method in `rounding.py`

, which doesn’t really round at all.

**01:56**
It only truncates values chopping the number off at the desired rounding point. All of my rounding functions take two arguments. `num`

is the number to be rounded, and `decimals`

is the number of significant digits.

**02:10**
Remember when I said that vague thing “pick the position to the right of where you want to round?” Well mathematically, the easiest way of doing that is to multiply the number by 10 to the power of the number of digits that you want in your precision. The value of decimals defaults to zero and 10 to the power of zero is one.

**02:30**
So with the default, you’re rounding to the one’s digit. In this next line, I’m converting a float to an integer because that causes the value to be truncated. So that I can truncate at any decimal position I first multiply, then convert, which does the truncation.

**02:48**
Then divide back down to the original magnitude. As an example, let’s think back to 1.4. Calling `truncate()`

will multiply 1.4 by one, which of course is 1.4.

**03:00**
Then convert 1.4, the float, to an int that truncates it to one. It then divides it by the multiplier, which was one and one divided by one is still one. And as this is a truncation method, even if the value was 1.9999, you’re still going to get one as a result.

**03:20**
Okay, with `truncate()`

in place, let’s do a little experiment.

**03:25**
Say you had a hundred dollars to play with on the stock market, and let’s say the market fluctuates randomly going up or down by some amount less than a nickel.

**03:34**
It does this once a second. To emulate this market in Python, I’m going to loop through 1 million seconds, adding or subtracting a number between zero and 0.05 to our $100.

**03:48**
At the end, we’ll see how much money is left. If the fluctuation is truly random, you should end up with something pretty close to a hundred bucks.

**04:02**
To show you how `truncate()`

can mess with math, I’m going to actually calculate two values, the fluctuation I just explained, and that same value truncated to three decimal places.

**04:13**
To do that latter part, I’ve imported the `truncate()`

function from our rounding module. Next, I need a random number, so I’ll import the random library.

**04:21**
Random values in computers aren’t actually random. They’re pseudorandom. That means the so-called random value is generated based on an algorithm, but if you start the algorithm from the same point, you’ll get the same list of values out of it.

**04:36**
This can be good and bad. It makes it easier to debug and test because you can get a predictable sequence of random values. Of course, if you really need an actual random number, predictable is bad.

**04:48**
So the gambling industry, for example, goes to a lot of work, making sure the starting point is unpredictable as well. To set the starting point for the random module, you call the `seed()`

function.

**05:01**
Now, if I ask for a series of random numbers, I’ll get them. If I call `seed()`

again, I will get the same list of numbers. So random but predictable, but not quite random, you get the idea. With that out of the way, let me store my $100 in a variable

**05:20**
and then do it again for the value that I’m going to truncate instead. That’s so you can see the difference. Now, I’m gonna loop a million times.

**05:32**
If you haven’t seen these underscores before, it’s a great little feature in Python, which makes integers more readable. It’s kind of like the position separator you might write in a number, be a comma or a period depending on where you live.

**05:45**
Inside this loop, I’m going to determine how much money is going to go up or down by asking for a random number between -0.05 and +0.05.

**06:01**
That fluctuation, I’m calling `delta`

. Then I’m going to actually apply the `delta`

to my value

**06:09**
and then I’m gonna do the same thing for the cut value. But instead, I’m going to truncate as I go along.

**06:19**
Alright, let it rip. And now let’s see how much money I made. Hmm, I’m down just over three bucks and 50 cents. I randomly got a few more negative deltas than positive ones.

**06:31**
If `random.uniform`

is fair over enough time, it should work out to a hundred bucks. Now let’s see how the truncated version did. Ooh, that’s not good.

**06:42**
That’s almost all my money gone. Simply by truncating to three decimal places I’ve lost over $99. Truncating is introducing a bias to our math. Dealing with this kind of bias is why there are a variety of rounding algorithms.

**06:56**
More on that later. To show you why you should round instead of truncating, let’s do the experiment again, but this time using Python’s `round()`

function.

**07:05**
Reset my `seed()`

so I get the same random values, so it’s a fair comparison.

**07:12**
Resetting my starting and storing a rounded version like cut

**07:29**
the same delta, calculate the actual like before, and now instead of calculating `cut`

, I’m going to calculate `rounded`

using the rounding method.

**07:46**
Once more into the breach,

**07:49**
Same seed, so the `actual`

is the same as before,

**07:53**
and when I `round`

instead of `cut`

, I get something that’s much better. It isn’t perfect, but by rounding to three decimal places instead of truncating, I’ve only lost a difference of about 20 cents.

**08:04**
This is why you round when you need to get rid of things instead of just truncating them completely.

**08:12**
Next up, we’ll start your journey across the land of rounding with rounding up. Get your hiking boots on, it’s a climb.

Become a Member to join the conversation.