`RoundTo.Rd`

Returns a number rounded to the nearest specified multiple.

`RoundTo(x, multiple = 1, FUN = round)`

- x
numeric. The value to round.

- multiple
numeric. The multiple to which the number is to be rounded. Default is 1.

- FUN
the rounding function as character or as expression. Can be one out of

`trunc`

,`ceiling`

,`round`

(default) or`floor`

.

There are several functions to convert to integers. `round`

rounds to the nearest integer or to any number of digits. Using a negative number rounds to a power of ten, so that `round (x, -3)`

rounds to thousands.
Each of `trunc`

, `floor`

and `ceiling`

round in a fixed direction, towards zero, down and up respectively. `round`

is documented to round to even, so `round(2.5)`

is `2`

.

`RoundTo`

uses `round(x/multiple)*multiple`

to get the result. So if `x`

is equally close to two multiples, the multiple with the smaller absolute value will be returned when `round(x/multiple)`

is even (and the greater when it's odd).

If `FUN`

is set to `ceiling`

it will always round up, and if set to `floor`

it will always round down. See examples for comparison).

the rounded value

```
RoundTo(10, 3) # Rounds 10 to a nearest multiple of 3 (9)
#> [1] 9
RoundTo(-10, -3) # Rounds -10 to a nearest multiple of -3 (-9)
#> [1] -9
RoundTo(1.3, 0.2) # Rounds 1.3 to a nearest multiple of 0.2 (1.2)
#> [1] 1.2
RoundTo(-1.3, 0.2) # Rounds -1.3 to a nearest multiple of 0.2 (-1.2)
#> [1] -1.2
RoundTo(5, -2) # Returns an error, because -2 and 5 have different signs
#> [1] 4
# Round down
RoundTo(c(1,-1) * 1.2335, 0.05, floor)
#> [1] 1.20 -1.25
RoundTo(c(1,-1) * 1233.5, 100, floor)
#> [1] 1200 -1300
# Round up
RoundTo(c(1,-1) * 1.2335, 0.05, ceiling)
#> [1] 1.25 -1.20
RoundTo(c(1,-1) * 1233.5, 100, ceiling)
#> [1] 1300 -1200
# Round towards zero
RoundTo(c(1,-1) * 1.2335, 0.05, trunc)
#> [1] 1.2 -1.2
RoundTo(c(1,-1) * 1233.5, 100, trunc)
#> [1] 1200 -1200
x <- c(-1.5,-1.3, 1.3, 1.5)
cbind(x = x,
round = RoundTo(x, 0.2, FUN=round),
trunc = RoundTo(x, 0.2, FUN=trunc),
ceiling = RoundTo(x, 0.2, FUN=ceiling),
floor = RoundTo(x, 0.2, FUN=floor)
)
#> x round trunc ceiling floor
#> [1,] -1.5 -1.6 -1.4 -1.4 -1.6
#> [2,] -1.3 -1.2 -1.2 -1.2 -1.4
#> [3,] 1.3 1.2 1.2 1.4 1.2
#> [4,] 1.5 1.6 1.4 1.6 1.4
x <- -10:10
cbind(x = x,
round = RoundTo(x, 2, FUN=round),
trunc = RoundTo(x, 2, FUN=trunc),
ceiling = RoundTo(x, 2, FUN=ceiling),
floor = RoundTo(x, 2, FUN=floor)
)
#> x round trunc ceiling floor
#> [1,] -10 -10 -10 -10 -10
#> [2,] -9 -8 -8 -8 -10
#> [3,] -8 -8 -8 -8 -8
#> [4,] -7 -8 -6 -6 -8
#> [5,] -6 -6 -6 -6 -6
#> [6,] -5 -4 -4 -4 -6
#> [7,] -4 -4 -4 -4 -4
#> [8,] -3 -4 -2 -2 -4
#> [9,] -2 -2 -2 -2 -2
#> [10,] -1 0 0 0 -2
#> [11,] 0 0 0 0 0
#> [12,] 1 0 0 2 0
#> [13,] 2 2 2 2 2
#> [14,] 3 4 2 4 2
#> [15,] 4 4 4 4 4
#> [16,] 5 4 4 6 4
#> [17,] 6 6 6 6 6
#> [18,] 7 8 6 8 6
#> [19,] 8 8 8 8 8
#> [20,] 9 8 8 10 8
#> [21,] 10 10 10 10 10
```