Round to multiple of any number.

round_any(x, accuracy, f = round)

Arguments

x

numeric or date-time (POSIXct) vector to round

accuracy

number to round to; for POSIXct objects, a number of seconds

f

rounding function: floor, ceiling or round

Examples

round_any(135, 10)
#> [1] 140
round_any(135, 100)
#> [1] 100
round_any(135, 25)
#> [1] 125
round_any(135, 10, floor)
#> [1] 130
round_any(135, 100, floor)
#> [1] 100
round_any(135, 25, floor)
#> [1] 125
round_any(135, 10, ceiling)
#> [1] 140
round_any(135, 100, ceiling)
#> [1] 200
round_any(135, 25, ceiling)
#> [1] 150

round_any(Sys.time() + 1:10, 5)
#>  [1] "2025-10-29 18:02:20 UTC" "2025-10-29 18:02:20 UTC"
#>  [3] "2025-10-29 18:02:20 UTC" "2025-10-29 18:02:25 UTC"
#>  [5] "2025-10-29 18:02:25 UTC" "2025-10-29 18:02:25 UTC"
#>  [7] "2025-10-29 18:02:25 UTC" "2025-10-29 18:02:25 UTC"
#>  [9] "2025-10-29 18:02:30 UTC" "2025-10-29 18:02:30 UTC"
round_any(Sys.time() + 1:10, 5, floor)
#>  [1] "2025-10-29 18:02:15 UTC" "2025-10-29 18:02:20 UTC"
#>  [3] "2025-10-29 18:02:20 UTC" "2025-10-29 18:02:20 UTC"
#>  [5] "2025-10-29 18:02:20 UTC" "2025-10-29 18:02:20 UTC"
#>  [7] "2025-10-29 18:02:25 UTC" "2025-10-29 18:02:25 UTC"
#>  [9] "2025-10-29 18:02:25 UTC" "2025-10-29 18:02:25 UTC"
round_any(Sys.time(), 3600)
#> [1] "2025-10-29 18:00:00 UTC"