Freigeben über


Small Basic Rounding Methods: Digging Deeper

The Math object gives you three methods to round numbers! We explain these methods in the Table below.

Table: Rounding methods available in the Math object

 Method

 Description

 Ceiling(number)

 Returns the integer that’s greater than or equal to the given decimal number

 Floor(number)

 Returns the integer that’s less than or equal to the given decimal number

 Round(number)

 Rounds a given number to the nearest integer

These examples show you output from the three methods in the Table above:

Math.Ceiling(3.1)  ' =  4

Math.Ceiling(3.0)  ' =  3

Math.Ceiling(-3.0) ' = -3

Math.Ceiling(-3.1) ' = -3

Math.Floor(3.1)    ' =  3

Math.Floor(3.0)    ' =  3

Math.Floor(-3.0)   ' = -3

Math.Floor(-3.1)   ' = -4

Math.Round(3.0)    ' =  3

Math.Round(3.1)    ' =  3

Math.Round(3.6)    ' =  4

Math.Round(-3.0)   ' = -3

Math.Round(-3.1)   ' = -3

Math.Round(-3.6)   ' = -4

Note: Be careful if you use the Round() method when the fraction part of the number is exactly 0.5. In this case, the Round() method rounds to the nearest even integer (this is called banker's rounding). For example, 0.5 and –0.5 are rounded to 0, 1.5 and 2.5 are rounded to 2.0, and –1.5 and –2.5 are rounded to –2. This is different from what you learned in Algebra, where the 0.5 fraction’s always rounded up to 1.

But wait! There’s a simple math trick! You can still use the Round() method to round a number to a given decimal place! You just need to turn your decimal number into an integer, round it, and then send it back to decimal town!

Let’s say for example that you want to round the number 3.6875 to the nearest tenth (the first position to the right of the decimal point). Here are your steps:

  1. Multiply the number by 10. That’s 36.875.
  2. Round your answer from step 1. You get 37.
  3. Divide the answer you got in step 2 by 10. Now you have 3.7.

You converted your decimal number into an integer, rounded it, and converted it back! You see? It’s a bit of a workaround, but let’s play with it some more! Here’s a Small Basic statement that follows our steps above:

x = Math.Round( 3.6875 * 10 ) / 10

Now let’s say you needto round the number 3.6875 to the nearest hundredth (that’s the second position to the right of the decimal point). Write it like this:

x = Math.Round( 3.6875 * 100 ) / 100

That statement gives you 3.69. You usually round to the nearest hundredths if you’re making an application that involves money (you know, the old green and metal stuff that’s virtually on your plastic things).

Let’s take a closer look at the Floor() method. The call to Math.Floor(x), for any number x, returns the greatest integer that’s less than or equal to x. When x is a positive number, Floor(x) cuts out (truncates) the decimal part of x, leaving only the integer part. Integer truncation is when you chop off the fractional part of a number. You see, truncation isn’t the same as rounding. For example, while Math.Floor(1.9) returns 1, Math.Round(1.9) returns 2.

You can use the same math trick that you used with the Round() method, to truncate up to any number of decimal parts. For example, if x=3.6875, you can keep three decimal digits by writing this statement:

Math.Floor(1000*x) / 1000

You can also use the Floor() method to round to the nearest integer. For example, to round a number, x, to the nearest integer, you write:

Math.Floor(x + 0.5)

Do you see what we did there? If x is 0.6, then you add 0.5 to it, making it 1.1 (the Floor() method then truncates it down to 1). And if you want to round your value for x to the nearest hundredth, write this:

Math.Floor(100*x + 0.5) / 100

Now you can control the number of decimal places displayed! Do you see that? You multiply x by 100, add 0.5 to round up, you truncate all the extra decimal numbers, and then you move the decimal back two spots to the left. Here’s an example:

x = 1/3

TextWindow.WriteLine( x ) ' Displays: 0.3333333333333333333333333333

TextWindow.WriteLine( Math.Floor(100*x + 0.5) / 100 ) ' Displays: 0.33

With the Floor() method, you can also test divisibility of positive integers (whether one number can be divided evenly by another number). If a number, x, is divisible by 5, then x/5 and Floor(x/5) are equal. This next code snippet shows you how to do this (please try it out):

x = 10  ' Divisible by 5

TextWindow.WriteLine( x/5 )               ' = 2

TextWindow.WriteLine( Math.Floor(x/5) )   ' = 2

x = 12  ' Not divisible by 5

TextWindow.WriteLine( x/5 )               ' = 2.4

TextWindow.WriteLine( Math.Floor(x/5) )   ' = 2

Since 10 is divisible by 5, then 10/5 and Floor(10/5) are the same (2 is the same as 2.0 with the decimal truncated). On the other hand, since 12 isn’t evenly divisible by 5, 12/5 isn’t equal to Floor(12/5). (12/5 is 2.4 and Floor(12/5) is 2, because the decimal’s truncated).

The Ceil() method is like paying for parking. If you parked your car for less than an hour, you’ll be charged for one hour. If your park time was between 1 and 2 hours, you’ll be charged for two hours (so 3.01 is rounded up to the next whole number, 4). Parking meters have no mercy!

Go to https://blogs.msdn.com/SmallBasic to download Small Basic for free and learn all about it!

Small and Basically yours,

   - Majed Marji & Ed Price

Comments