Rounding

Rounding is the representation of a number in a shorter, simpler or more explicit way. Its intended purpose is to replace any number with a shorter representation of it. It is important to remember that you are manipulating that number and what that manipulated representation is meant to convey. The root of the issue is simple. When you round a number, you are changing the number.

If you do not take care to think about what the number represents while rounding, you may represent it incorrectly. For example, when you round time erroneously you can create results with a time that has not yet happened, or you may make certain results inaccessible when working off those rounded numbers. Depending on the application this can yield very erratic and unexpected behavior. I have examples that show this below, both of which are places where classic rounding toward the nearest whole number is inappropriate to use. The first example is representing time as a percentage of an elapsed total.

Let’s say you are displaying in an application the percent of the year that is complete, with 100% being midnight on December 31st. For this math, a full day represents 0.27397260273972602739726% (assuming a 365 day year). This means on December 30th, at 6am the year is 99.5205479452054794520547% complete. This number is already rounded. Now if that number was rounded again before it was displayed to turn it into an integer, it would be showing as 100% complete with over 1 day and 18 hours to go before the year is actually completed.

The aforementioned example shows that the larger the duration of time you are representing, the more prominent the error. This function should then utilize the floor() method. This is still a type of rounding, but more accurate and appropriate to this use case. When you use the floor() method, you are rounding down, or rather rounding towards minus infinity. For the instance of relative time, in general you want to protect against displaying time in the future when you are reacting to something happening now.

The next example is a search result. In this example, let us assume that a user is searching for something and you are displaying results in pages of 30 entries. Given a search yielding 462 results, we would expect 15.4 pages to be displayed. We cannot show 0.4 pages, so we need to round that to an integer. Following the example from before, let’s use the floor() method. This now has 15 pages. Whoops! Now you have 12 results that are inaccessible, since they are on the 0.4 pages that you have culled via rounding error since you only rendered pages 1 through 15.

This example shows when you need to utilize the ceil() or “ceiling” function. This function is also a type of rounding. Any results above the last integer require an additional integer to function correctly. Therefore, we must round the remainder up to create a wholly inclusive set. When you use ceil() you are rounding up, or towards plus infinity.

There are other examples of irresponsible rounding, but the important takeaway is this: Rounding is replacing one number with another number. Hopefully when you think about it that way, it will give you pause to think about how best to represent that number. While the examples above are fairly straightforward, rounding errors generally accumulate and can render whole results meaningless when inappropriate rounding functions are applied.

The feature image for this post is from pixabay

Author: Axe
I am a senior developer. Ride my motorcycle whenever I can. Love photography, hiking, camping, cooking, coding, and cheese cake!

Leave a Reply