DescTools (version 0.99.18)

RoundTo: Round to Multiple

Description

Returns a number rounded to the desired multiple.

Usage

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

Arguments

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 ceiling, round (default) or floor.

Value

Details

RoundTo rounds up, away from zero, if the remainder of dividing number by multiple is greater than or equal to half the value of multiple if FUN is set to round. If FUN is set to ceiling it will always round up, and if set to floor it will always round down.

See Also

round, ceiling, floor

Examples

Run this code
RoundTo(10, 3)     # Rounds 10 to a nearest multiple of 3 (9)
RoundTo(-10, -3)   # Rounds -10 to a nearest multiple of -3 (-9)

RoundTo(1.3, 0.2)  # Rounds 1.3 to a nearest multiple of 0.2 (1.2)
RoundTo(5, -2)     # Returns an error, because -2 and 5 have different signs

# Round down
RoundTo(c(1,-1) * 1.2335, 0.05, floor)
RoundTo(c(1,-1) * 1233.5, 100, floor)

# Round up
RoundTo(c(1,-1) * 1.2335, 0.05, ceiling)
RoundTo(c(1,-1) * 1233.5, 100, ceiling)

# Round towards zero
RoundTo(c(1,-1) * 1.2335, 0.05, trunc)
RoundTo(c(1,-1) * 1233.5, 100, trunc)

Run the code above in your browser using DataCamp Workspace