hard_round: Round values in a vector to sum properly

Description Usage Arguments Examples

View source: R/hard_round.R

Description

Takes a vector of values that are meant to be added together and rounds them in such a way that the sum of each of the rounded values equals the rounded sum of the unrounded values. For example, c(1.2, 1.3, 1.4) sums to 3.9, which becomes 4 when rounded to the nearest integer. However, rounding each of the values to the nearest integer produces c(1, 1, 1), which only sums to 3. This function will "hard round" the 1.4 up to 2, so that the resulting vector c(1, 1, 2) sums to 4. This is a common practice in preparing financial statements.

Usage

1

Arguments

x

A numeric vector of values to be rounded.

units

A number indicating the units to which to round. Unlike R's round function which takes the argument digits and only allows you to round to decimal places, hard_round takes the actual unit of rounding, e.g. 1 for nearest integer, 10 for nearest ten, 0.1 for nearest tenth. It can also round to non-decimal units, such as 5 or 0.2.

Examples

1
hard_round(x = c(1.2, 1.3, 1.4), units = 1)

Prometheus77/actools documentation built on March 7, 2020, 11:01 a.m.