Round


The Round function converts a number to the nearest integer.

Syntax

CRect:Round()

  

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 correctly 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

-- rounds to -3

n[2] = -2.50000001

-- rounds to -3

n[3] = -2.5

-- rounds to -2

n[4] = -2.49999999

-- rounds to -2

n[5] = -2

-- rounds to -2

n[6] = -1.50000001

-- rounds to -2

n[7] = -1.5

-- rounds to -2

n[8] = -1.49999999

-- rounds to -1

n[9] = -1

-- rounds to -1

n[10] = -0.5000001

-- rounds to -1

n[11] = -0.5

-- rounds to 0

n[12] = -0.4999999

-- rounds to 0

n[13] = 0

-- rounds to 0

n[14] = 0.4999999

-- rounds to 0

n[15] = 0.5

-- rounds to 0

n[16] = 0.5000001

-- rounds to 1

n[17] = 1

-- rounds to 1

n[18] = 1.4999999

-- rounds to 1

n[19] = 1.5

-- rounds to 2

n[20] = 1.5000001

-- rounds to 2

n[21] = 2

-- rounds to 2

n[22] = 2.4999999

-- rounds to 2

n[23] = 2.5

-- rounds to 2

n[24] = 2.5000001

-- rounds to 3

n[25] = 3

-- rounds to 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