Skip to content

Commit

Permalink
Add function for rounding half to largest ("math rounding"). Issue #100
Browse files Browse the repository at this point in the history
  • Loading branch information
gdemin committed Jun 24, 2023
1 parent 480a18f commit d884661
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
17 changes: 15 additions & 2 deletions R/aaa_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,28 @@ flat_list=function(x, flat_df = FALSE){
}
}


# contrary to base 'round' round .5 to bigest integer
round2 = function(x, digits = 0) {
posneg = sign(x)
z = abs(x)*10^digits
z = z + 0.5 + sqrt(.Machine$double.eps)
z = trunc(z)
z = z/10^digits
z*posneg
}

## round all numerics in the data.frame
round_dataframe = function(x, digits = NULL){
if(is.null(digits)) digits = 1
if(is.na(digits)) return(x)
if(isFALSE(get_expss_rounding())) {
round_function = round2
} else {
round_function = round
}
for (i in seq_len(NCOL(x))){
if(is.numeric(x[[i]])){
x[[i]] = round(x[[i]], digits)
x[[i]] = round_function(x[[i]], digits)
}
}
x
Expand Down
39 changes: 27 additions & 12 deletions man/expss.options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d884661

Please sign in to comment.