-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmyround.R
44 lines (40 loc) · 898 Bytes
/
myround.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# myround
#'
#' Round a number, preserving extra 0's
#'
#' Round a number, preserving extra 0's.
#'
#' @param x Number to round.
#'
#' @param digits Number of digits past the decimal point to keep.
#'
#' @details
#' Uses [base::sprintf()] to round a number, keeping extra 0's.
#'
#' @export
#' @return
#' A vector of character strings.
#'
#' @examples
#' myround(51.01, 3)
#' myround(0.199, 2)
#'
#' @seealso [base::round()], [base::sprintf()]
#'
#' @keywords
#' utilities
myround <-
function(x, digits=1)
{
if(digits < 1)
stop("This is intended for the case digits >= 1.")
if(length(digits) > 1) {
digits <- digits[1]
warning("Using only digits[1]")
}
tmp <- sprintf(paste("%.", digits, "f", sep=""), x)
# deal with "-0.00" case
zero <- paste0("0.", paste(rep("0", digits), collapse=""))
tmp[tmp == paste0("-", zero)] <- zero
tmp
}