Round


The Round function converts a number to the nearest integer.

Syntax

CRect:Round()

  • This method has no arguments and no return value.

Remarks

The number x is rounded to integer n if (n – 0.5) <= x < (n + 0.5). Half integers n+0.5 are rounded up or down in the standard way to avoid statistical bias. The strategy extends across the 0 boundary to negative numbers.

Example

The following script lists the rounded values of some numbers in the range -3 through +3. You can run this script and modify it to test rounding on your machine.

n = {}

-- create a table to hold the values

n[1] = -3

 

n[2] = -2.50000001

 

n[3] = -2.5

-- should give -2

n[4] = -2.49999999

 

n[5] = -2

 

n[6] = -1.50000001

 

n[7] = -1.5

-- should give -2

n[8] = -1.49999999

 

n[9] = -1

 

n[10] = -0.5000001

 

n[11] = -0.5

-- should give 0

n[12] = -0.4999999

 

n[13] = 0

 

n[14] = 0.4999999

 

n[15] = 0.5

-- should give 0

n[16] = 0.5000001

 

n[17] = 1

 

n[18] = 1.4999999

 

n[19] = 1.5

-- should give 2

n[20] = 1.5000001

 

n[21] = 2

 

n[22] = 2.4999999

 

n[23] = 2.5

-- should give 2

n[24] = 2.5000001

 

n[25] = 3

 

 

 

for i = 1, table.getn(n) do

-- list all table array elements

  Printf("%.10lg --> %lg\n", n[i], Round( n[i] ) )

end

 

Related Topics

CRect:Floor