Loading video player…

Rounding Up

00:00 In the previous lesson, I showed you truncation and why you want to round instead. In this lesson, I’ll begin to explore the different ways of rounding, starting with rounding up To better understand why Python has chosen the algorithm it has for the built-in round() function, over the next couple of lessons, I’m going to show you different ways of doing rounding.

00:20 I’m starting with rounding up. In this case, any value other than zero in the rounding position causes an increment of the next position to the left before zeroing everything else out.

00:32 Consider the number 12.345. Rounding up in the tens position gives 20. That’s because the value in the ones position is bigger than zero, so the value in the tens position gets bumped up.

00:44 Rounding up in the ones position results in 13 because the three in the tens position is bigger than zero. Four in the hundredths position means rounding on the tenths gives you 12.4 and likewise, five bumps the four up one as well.

01:02 Let’s look at some code that does this.

01:06 To implement rounding up I’m going to use the ceil() function in the math module.

01:12 ceil() is short for ceiling, which means to return the nearest integer greater than or equal to a given number. The ceil() of 3.7 is four.

01:25 ceil() on a round number is itself, which makes sense as the closest integer to two that’s greater than or equal to two is two. ceil() on negative 0.5 is zero.

01:41 Notice that it isn’t negative one. The closest integer, greater or equal to, means going up; up from negative 0.5 is zero. Let’s do another negative.

01:55 And again, that greater than or equal to thing means going up, going to the right. Let’s build this into a round_up() function.

02:06 I’m back in rounding.py and like with truncate(), I’m multiplying by a power of 10 to get at a specific decimal place before I start the rounding.

02:16 Also, like truncate(), I multiply, but this time, instead of chopping the value, I’m calling ceil() on it. Then I divide it again to restore the number back to its original magnitude.

02:28 Let’s try this out. Importing

02:36 round_up(1.1), that’s two. Let’s test this with a non-default decimal position.

02:46 1.23 to the tenths is 1.3. Again, rounding up.

02:54 There’s some hundredths looks good. One side effect of using the power of 10 multiplier is that you get rounding on the left of the decimal place for free by passing in a negative number.

03:07 22.45 rounding negative one decimal places gives you 30. Alright, that’s enough positivity. Let’s be a little more negative. And once again, because of the use of ceil(), negative numbers trend towards zero when you round them up heading to the right. One way to think about the negative number situation is to visualize a number line.

03:34 Rounding up means moving to the right on the line. Any value that isn’t dead on an integer is going to move rightwards. For positive numbers, that means a bigger value. For negative numbers that means a smaller negative heading towards zero.

Become a Member to join the conversation.